Q:

C program to decimal to binary number using recursion

belongs to collection: Conversion Programs in C

0

In this exercise, we learn how to write a C program to decimal to binary number using recursion?. We will write the C program to decimal to binary number using recursion. Write a C program to input the decimal number and convert it to a binary number using recursion. How to convert decimal to binary number in C programming using recursion. Logic to convert decimal to binary number in C using recursion.

Example,

Input: 5
Output: 101
 
 
Input: 9
Output: 1001

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

The below program ask the user to enter the decimal number. After getting the value from the user it will convert the decimal number in a binary number.

#include <stdio.h>
// Recursive function to convert n
// to its binary equivalent
int decimalToBinary(int n)
{
    if (n == 0)
    {
        return 0;
    }
    else
    {
        return (n % 2 + 10 *
                decimalToBinary(n / 2));
    }
}
int main()
{
    //num for decimal number
    int num;
    printf("Enter decimal number: ");
    scanf("%d", &num);
    //Called function
    printf ("%d",decimalToBinary(num));
    return 0;
}

Output:

Enter decimal number: 34
100010

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C program to convert decimal to binary without usi... >>
<< C program to decimal to binary using recursion and...