C Program to Convert a Decimal Number to Binary & Count the Number of 1s
In this program, I have discussed about the C Program to Convert a Decimal Number to Binary & Count the Number of 1s
#include<stdio.h>
int main()
{
long num,binary=0,decimal,base=1,rem,n1=0;
printf("Enter a decimal number:");
scanf("%d",&num);
decimal=num;
while(num>0)
{
rem=num%2;
if(rem==1)
{n1++;
}
binary=binary+rem*base;
num=num/2;
base=base*10;
}
printf("The decimal number is %d\n",decimal);
printf("The binary number is %d\n",binary);
printf("No. of 1's in the binary number is %d\n",n1);
}
Just copy and paste the program in your desired compiler (Dev C++ is more preferable) and run it.
Enjoy programming in C.
0 comments:
Post a Comment