Source code:
//Write a program in C to convert a decimal number to binary using recursion.
#include<stdio.h>
long convertB_to_D(int decimal)
{
static long binary,remainder,multiplier = 1;
if(decimal != 0)
{
remainder = decimal % 2;
binary = binary + remainder * multiplier;
multiplier = multiplier * 10;
convertB_to_D(decimal / 2);
}
return binary;
}
int main()
{
long binary;
int decimal;
printf("Enter a decimal number: ");
scanf("%d",&decimal);
binary = convertB_to_D(decimal);
printf("The Binary value is : %ld\n", binary);
return 0;
}
No comments:
Post a Comment