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

The depression in freezing point of water observed for the same amount of acetic acid, trichloroacetic acid and trifluoroacetic acid increases in the order given above. Explain briefly.

  Depression in freezing point is a colligative property that depends on the number of solute particles in a solution. Since the order of de...