In this blog post, 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
C program to decimal to binary number using recursion:
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