Posted by: Sourav | August 1, 2014

Decimal to Binary using recursion in C


 

#include<stdio.h>
#include<math.h>
void binary(int);
void main()
{
 int sum;

 binary(21);
 
//printf(“%d”,result);

 

 

 

}
void binary(int i)
{
 
 int remain;
 static int sum=0;
 static int j=0;
  //printf(“%d”,i);
 if(i>0)
 {

  remain=(i%2);
  i=i/2;
   //printf(“\n%d”,remain);
  sum=sum+(remain*(pow(10,j)));
  j++;
  binary(i);
 }
 else
 {
 printf(“%d”, sum);
 }
 
 

}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: