C Program to Convert a Number Decimal System to Binary System using Recursion

«
»

This is a C program to convert a number decimal system to binary system using recursion.

Problem Description

The following C program using recursion finds a binary equivalent of a decimal number entered by the user.

Problem Solution

The user has to enter a decimal which has a base 10 and this program evaluates the binary equivalent of that decimal number with base 2.

advertisement
Program/Source Code

Here is the source code of the C program to find the binary equivalent of the decimal number. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*  
 * C Program to Convert a Number Decimal System to Binary System using Recursion
 */
#include <stdio.h>
 
int convert(int);
 
int main()
{
    int dec, bin;
 
    printf("Enter a decimal number: ");
    scanf("%d", &dec);
    bin = convert(dec);
    printf("The binary equivalent of %d is %d.\n", dec, bin);
 
    return 0;
}
 
int convert(int dec)
{
    if (dec == 0)
    {
        return 0;
    }
    else
    {
        return (dec % 2 + 10 * convert(dec / 2));
    }
}
Program Explanation

In this C program, we are reading a decimal number using the ‘dec’ variable. The function convert() is used to convert a number decimal system to binary system using recursion.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

If else condition statement is used to check the value of ‘dec’ variable is equal to 0, if the condition is true execute the statement return null value. Otherwise if the condition is false then execute the else condition statement.

Divide the value of ‘dec’ variable by 2. Multiply the result value with 10 and add the value with the modulus of the value of ‘dec’ variable by 2. Print the Binary number of the decimal system using printf statement.

Runtime Test Cases
 
$ cc pgm31.c
$ a.out
Enter a decimal number: 10
The binary equivalent of 10 is 1010.

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Check this: C Books | BCA MCQs
advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Simple C Programs, go to Simple C Programs. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.