Tuesday, May 31, 2016

C program to print binary representation of number

#include<stdio.h>
#include<math.h>
//function to print binary number of a decimal
void binary(int i)
{
 if(i!=0)
  {
   binary(i>>1);
  }
 printf("%c",i&1==1?'1':'0');
}
void main()
{
 int i,k,l,j=0;
 printf("\nEnter a no");
 scanf("%d",&i);
 l=i;
 //To print binary equivalent
 printf("\nBinary representation of the no is ");
 binary(l);
}

output
Enter a no 54
Binary representation of the no is 110110

No comments:

Post a Comment