C Program to Print Binary Equivalent of an Integer using Recursion

This is a C program to print binary equivalent of an integer using recursion.

Problem Description

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

Problem Solution

Decimal numbers are of base 10 while binary numbers are of base 2.

Program/Source Code

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

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

In this C program, we are reading a decimal number using ‘num’ variable. Decimal numbers are of base 10, while binary numbers are of base 2. The binary_conversion() function is used to find the binary equivalent of a decimal number entered by the user.

advertisement
advertisement

In binary_conversion() function, convert the binary number to its equivalent decimal value. If else condition statement is used to check the value of ‘num’ variable is equal to 0. If the condition is true, execute the statement by returning 0 to the called variable ‘bin’.

Otherwise if the condition is false, execute else statement. Compute the modulus of the value of ‘num’ variable by 2 and add the resulted value to 10. Multiply the resulted value with the value of binary_conversion() function. Divide the value of ‘num’ variable by 2 and pass as an argument and execute the function recursively. Print the Binary equivalent of an integer using recursion.

Runtime Test Cases
 
$ gcc binary_recr.c -o binary_recr
$ a.out
Enter a decimal number: 10
The binary equivalent of 10 is 1010

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

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 & discussions at Telegram SanfoundryClasses.