C Program to Print Binary Equivalent of an Integer using Recursion


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

0 comments:

Post a Comment

 
Top