Advertisement
skashminzim

decimal to binary using recursion

Sep 22nd, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.39 KB | None | 0 0
  1. #include<stdio.h>
  2. int convert(int);
  3.  
  4. int main()
  5. {
  6.     int dec, bin;
  7.  
  8.     printf("Enter a decimal number: ");
  9.     scanf("%d", &dec);
  10.     bin = convert(dec);
  11.     printf("The binary equivalent of %d is %d.\n", dec, bin);
  12.  
  13.     return 0;
  14. }
  15.  
  16. int convert(int dec)
  17. {
  18.     if (dec == 0)
  19.     {
  20.         return 0;
  21.     }
  22.     else
  23.     {
  24.         return (dec % 2 + 10 * convert(dec / 2));
  25. }  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement