Image

Programs - C Language - Recursion

Program:

#include<stdio.h>
#include<conio.h>
long toBinary(int);
void main(){
long binaryNo;
int decimalNo;

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

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

 getch(); 
}

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