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:

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

    ReplyDelete
    Replies
    1. Programming in C - Kernigan & Ritchie

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

    ReplyDelete
  3. What does “static” mean in a C program?

    ReplyDelete
  4. how can you convert decimal array into binary?

    ReplyDelete
  5. 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

    ReplyDelete
  6. #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--;
    }

    ReplyDelete
  7. 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;
    }

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

    ReplyDelete
  9. Ammar yasir5/11/14, 7:03 PM

    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;
    }

    ReplyDelete
  10. 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));
    }

    ReplyDelete
  11. 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);
    }
    }

    ReplyDelete
  12. Program for Decimal to Roman Number conversion

    Program for Decimal to Roman Number conversion

    #include

    main()
    {
    int a,b,c,d,e;
    clrscr();
    printf("Input a number (between 1-3000):");
    scanf("%d",&e);
    while (e==0||e>3000)
    {
    printf ("ERROR: Invalid Input!
    ");
    printf ("Enter the number again:");
    scanf ("%d",&e);
    }
    if (e>3000)
    printf("Invalid");
    a = (e/1000)*1000;
    b = ((e/100)%10)*100;
    c = ((e/10)%10)*10;
    d = ((e/1)%10)*1;

    if (a ==1000)
    printf("M");
    else if (a ==2000)
    printf("MM");
    else if (a ==3000)
    printf("MMM");

    if (b == 100)
    printf("C");
    else if (b == 200)
    printf("CC");
    else if (b == 300)
    printf("CCC");
    else if (b == 400)
    printf("CD");
    else if (b ==500)
    printf("D");
    else if (b == 600)
    printf("DC");
    else if (b == 700)
    printf("DCC");
    else if (b ==800)
    printf("DCCC");
    else if (b == 900)
    printf("CM");


    if (c == 10)
    printf("X");
    else if (c == 20)
    printf("XX");
    else if (c == 30)
    printf("XXX");
    else if (c == 40)
    printf("XL");
    else if (c ==50)
    printf("L");
    else if (c == 60)
    printf("LX");
    else if (c == 70)
    printf("LXX");
    else if (c ==80)
    printf("LXXX");
    else if (c == 90)
    printf("XC");

    if (d == 1)
    printf("I");
    else if (d == 2)
    printf("II");
    else if (d == 3)
    printf("III");
    else if (d == 4)
    printf("IV");
    else if (d ==5)
    printf("V");
    else if (d == 6)
    printf("VI");
    else if (d == 7)
    printf("VII");
    else if (d ==8)
    printf("VIII");
    else if (d == 9)
    printf("IX");
    getch();
    }

    ReplyDelete