C Program to Print Binary Equivalent of an Integer using Recursion

Advertisements
This C program, using recursion, finds the binary equivalent of a decimal number entered by the user. Decimal numbers are of base 10 while binary numbers are of base 2.

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.

  1. /*
  2.  * C Program to Print Binary Equivalent of an Integer using Recursion
  3.  */
  4. #include <stdio.h>
  5.  
  6. int binary_conversion(int);
  7.  
  8. int main()
  9. {
  10.    int num, bin;
  11.  
  12.    printf("Enter a decimal number: ");
  13.    scanf("%d", &num);
  14.    bin = binary_conversion(num);
  15.    printf("The binary equivalent of %d is %d\n", num, bin);
  16. }
  17.  
  18. int binary_conversion(int num)
  19. {
  20.     if (num == 0)
  21.     {
  22.         return 0;
  23.     }
  24.     else
  25.     {
  26.         return (num % 2) + 10 * binary_conversion(num / 2);
  27.     }
  28. }}


$ 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.

Here’s the list of Best Reference 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.
Advertisements
Subscribe Newsletter & Posts

Manish Bhojasia, a technology veteran with 17+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer and SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage & Cluster Administration, Advanced C Programming, SAN Storage Technologies, SCSI Internals and Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him below.

Practice Interview Questions & Answers, Quizzes, Objective, Multiple Choice Aptitude Tests for Freshers and Experienced People in these topics - Learn C, C++, Java and C# Programming with coding example on Simple problems as well as tough Algorithms and Data-structures along with runtime output: - C Programs, Data-Structures and Algorithms, C++ Algorithms, Java Algorithms, C# Programs, Android Programs in Java

C Tutorials and Linux Commands Tutorial - C Tutorials, Linux Commands

Career Mentoring with our Founder / CTO - Read More
Advertisements

Follow Manish & Sanfoundry