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

Sacred Thought

5 May 2024 Hari Om Verse 50-51, chapter two:  In this chapter two Shree krishna explains a simple way of living. Free from desires and void ...