Decimal to binary conversion in c using recursion





C code to convert decimal number to binary number by recursion:

#include<stdio.h>

long toBinary(int);

int main(){

    long binaryNo;
    int decimalNo;

    printf("Enter any decimal number: ");
    scanf("%d",&decimalNo);

    binaryNo = toBinary(decimalNo);
    printf("Binary value is: %ld",binaryNo);

    return 0;
}

long toBinary(int decimalNo){

    static long binaryNo,remainder,factor = 1;

    if(decimalNo != 0){

         remainder = decimalNo % 2;
         binaryNo = binaryNo + remainder * factor;
         factor = factor * 10;
         toBinary(decimalNo / 2);
    }

    return binaryNo;
}

Sample output:

Enter any decimal number: 10
Binary value is: 1010

C code to convert decimal number to binary number without recursion:

#include<stdio.h>

long toBinary(int);

int main(){

    long binaryNo;
    int decimalNo;

    printf("Enter any decimal number: ");
    scanf("%d",&decimalNo);

    binaryNo = toBinary(decimalNo);
    printf("Binary value is: %ld",binaryNo);

    return 0;
}

long toBinary(int decimalNo){

    long binaryNo,remainder,factor = 1;

    while(decimalNo != 0){
         remainder = decimalNo % 2;
         binaryNo = binaryNo + remainder * factor;
         factor = factor * 10;
         decimalNo = decimalNo / 2;
    }

    return binaryNo;
}





Sum of n numbers using recursion in c
Matrix multiplication using recursion in c
Multiplication using recursion in c
Lcm using recursion in c
Using recursion in c find the largest element in an array
Prime number program in c using recursion
Decimal to binary conversion in c using recursion
C program for fibonacci series using recursion
Reverse a string using recursion
Write a program for palindrome using recursion
Find factorial of a number using recursion in c program
Find gcd of a number using recursion in c program
Find sum of digits of a number using recursion using cprogram
Find power of a number using recursion using c program
Binary search through recurssion using c program
Reverse a number using recursion in c program
Big list of c program examples

14 comments:

nishant said...

Thanks,
this program help me alot.
would you please tell me best book to learn C.

Anonymous said...

'the complete reference'-by Herbert Schildt.
'let us c'-by yashwant kanetkar.
'programming in ANSI C'-by E Balaguruswamy.

Anonymous said...

njdskl

Unknown said...

What does “static” mean in a C program?

Unknown said...

how can you convert decimal array into binary?

Anonymous said...

static means once we gave static variabe a value...then in the whole execution..the value is hold by the variable even if the it not in the scope

aNiSh KuMaR said...

#include
int findgcd(int,int);
int main()
{
int i;
int j=0;
int a[10];
printf("enter a number:");
scanf("%d",&i);
while(i)
{
int n=i%2;
a[j]=n;
j++;
i=i/2;
}
j--;
while(j>=0)
{
printf("%d",a[j]);
j--;
}

Anonymous said...

Won't work for numbers greater that 1023
you could use following code
#include
void to_binary(int n)
{
int remainder;
if(n != 0)
{
remainder = n % 2;
to_binary(n/2);
printf("%d", remainder);
}
}
int main()
{
int n;
scanf("%d", &n);
to_binary(n);
return 0;
}

Anonymous said...

Programming in C - Kernigan & Ritchie

Unknown said...

binaryNo is not initialized to anything. should not it return a garbage value?

Ammar yasir said...

i have a very simple and shortest code to find binary of any number

/*********************************************************************************/
/********** binary conversion through recursion *******************************/
/*********************************************************************************/
// by Ammar yasir
// Elect.Engineering student
// NAmal College Mianwali
// may 11, 2014

#include
int binary(int num)
{
int z;
if(num==1)
{
printf("%d",num);
return 1;
}
z=num%2;
num=num>>1;
printf(" %d ",z,binary(num));
}
int main()
{
int num;
printf("enter a number ");
scanf("%d",&num);
binary(num);
return 0;
}

Unknown said...

are you kidding me?
int convert(int num,int factor,int src,int dst){
return (!num)?0:(factor*(num%dst)+convert(num/dst,factor*src,src,dst));
}

ilikepcm said...

This is one of the simplest one :



/*Program to print binary equivalent of any number*/
#include
int binary(int);
void main()
{
int num;
printf("Enter any number :\n");
scanf("%d", &num);
printf("Binary equivalent of %d is : ", num);
binary(num);
printf("\n");
}
int binary(int n)
{
if(n==0)
{
printf("0");
return 0;
}
else
{
binary(n/2);
printf("%d",n%2);
}
}

Unknown said...

int binary(int dec){
int rem=dec%2;
if(dec==1 || dec==0)
return dec;
else return rem + (binary(dec/2)*10);
}