Saturday, 5 October 2013

Convert decimal to binary number

This is very basic program .:: just follow
::- see how to convert decimal number to binary..

Note:- we consider only integer decimal number.

Procedure :- Just simply divide decimal number by 2 as shown in figure & write remainder in reverse order .


C code :-

 /*to convert dicimal no into binary number */  
 #include<stdio.h>  
 void deci2binary(int);  
 main()  
 {  
      int n;  
      printf("\n input decimal number which have to convert to decimal: ");  
      scanf("%d",&n);  
      deci2binary(n);  
 }  
 void deci2binary(int n)  
 {  
      static int a=1,binary=0;  
      while(n>=1)  
       {  
            binary=binary + ((n%2)*a);   
             n=n/2;   // since n is int so round of to int  
            a=a*10;  
       }  
       binary=binary + a*n;  
       printf("\n requred binary no is : %d",binary);  
 }  

OUTPUT :-

No comments:

Post a Comment

THANKS FOR UR GREAT COMMENT

Blogger Widgets