Code.GeekInterview.com
 
Code Samples C
 

Count Numbers in Binary Number


Code ResourceAuthor: kaivalya1989  

Difficulty Level: Intermediate

Published: 15th Jul 2008   Read: 12719 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

this code is used to count the number of 0's and 1's in a binary number.

 


Sample Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. int n,x,a,b,s;
  6.  clrscr();
  7.  a=0;b=0;
  8.  printf("enter the binary no:");
  9.  scanf("%d",&n);
  10.  s=n;
  11.  while(n>0)
  12.   {
  13.    x=n%10;
  14.    n=n/10;
  15.    if(x==0)
  16.    a++;
  17.    else if(x==1)
  18.     b++;
  19.     }
  20.      printf("
  21. the number is:%d",s);
  22.      printf("
  23. the ones are:%d",b);
  24.      printf("
  25. the zeros are:%d",a);
  26.     getch();
  27.   }
Copyright GeekInterview.com


Next Article: Swaping two variables without using 3rd variable


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. kaivalya198932312
2. mano.mithun19248
3. deepu0817246
4. meefriend4ever7824
5. chandrikakr7815
6. shashivaishnav6687
7. sadasivathavamani6563
8. venkatakrishnansvpr6437
9. Jimmy Zorald6277
10. sripri5431

Active Coders

Refined Tags