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

11 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