Monday, July 2, 2018

C program to count number of bits in a decimal number

#include <stdio.h>

#include<stdint.h>

void main()

{

  unsigned long  long int num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0,no_of_0s=0;
    //int64_t num, decimal_num, remainder, base = 1, binary = 0, no_of_1s = 0,no_of_0s=0;



    printf("Enter a decimal integer \n");

    scanf("%lld", &num);

    decimal_num = num;

    while (num > 0)

    {

        remainder = num % 2;

        /*  To count no.of 1s */

        if (remainder == 1)

        {

            no_of_1s++;

        }
        else{
            no_of_0s++;
        }

        binary = binary + remainder * base;

        num = num / 2;

        base = base * 10;

    }

    printf("Input number is = %lld\n", decimal_num);

    printf("Its binary equivalent is = %lld\n", binary);

    printf("No.of 1's in the binary number is = %lld\n", no_of_1s);

    printf("No.of bits's in its binary equivalent is = %lld\n", no_of_1s+no_of_0s);
}

No comments:

Post a Comment

इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया - सुदर्शन फ़ाकिर

 इश्क में ग़ैरत-ए-जज़्बात ने रोने ना दिया वरना क्या बात थी किस बात ने रोने ना दिया आप कहते थे कि रोने से ना बदलेंगे नसीब उमर भर आप की इस बात...