Sunday, October 18, 2015

C Program to convert a decimal number in to binary and count the number of zeros and ones.

/* =====================================================================================
 * *
 * *       Filename:  bincount.c
 * *        Created:  Tuesday 3 February 2015
 * *       Compiler:  gcc
 * *         Author:  Manoj Kumar Patra,manojpatra.sit@gmail.com
 * *   Organization:  SCIS,University of Hyderabad
 * *
 * * =====================================================================================*/

#include<stdio.h>
int main()
{
int dn,quo,k=0,m;
int zcount=0,ocount=0;
int binaryNumber[100],bin[100],i=1,j;
printf("------------------------------------------\n");
printf("Enter any decimal number: ");
scanf("%d",&dn);
quo = dn;
while(quo!=0)
{
binaryNumber[i++]= quo % 2;
quo = quo / 2;
}
printf("\nEquivalent binary value of decimal number %d: ",dn);
for(j = i -1 ;j> 0;j--)
{
printf("%d",binaryNumber[j]);
bin[k]=binaryNumber[j];
k++;
}
printf("\n------------------------------------------");
for(m=0 ;m<k; m++)
{
if(bin[m]==0)
{
zcount++;
}
else
{
ocount++;
}
}
printf("\nThe number of ZERO present in the binary number: \t %d\n",zcount);
printf("The number of ONE present in the binary number: \t %d\n",ocount);
printf("\n------------------------------------------\n");
return 0;
}

OutPut:

No comments:

Post a Comment