decimal to binary conversion using recursion with C

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

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Menu