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
Post a Comment