Write C program to find reverse of a number using recursion


Introduction

I have used Code::blocks 12 compiler for debugging purpose. But you can use any C programming language compiler as per your availability.


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

Write C program to find reverse of a number using recursion


  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4.  
  5. //Fuction declaration
  6. int reverseNumber(int num);
  7.  
  8.  
  9. int main()
  10. {
  11. int num, reverse;
  12.  
  13. // Inputting number from user
  14. printf("Enter any number: ");
  15. scanf("%d", &num);
  16.  
  17. // Calling function to reverse any number
  18. reverse = reverseNumber(num);
  19.  
  20. printf("Reverse of %d : %d", num, reverse);
  21.  
  22. return 0;
  23. }
  24.  
  25.  
  26.  
  27. //Recursive function to find reverse of any number
  28.  
  29. int reverseNumber(int num)
  30. {
  31. // Find total digits in num
  32. int digit = (int) log10(num);
  33.  
  34. // Base condition
  35. if(num == 0)
  36. return 0;
  37.  
  38. return ((num%10 * pow(10, digit)) + reverseNumber(num/10));
  39. }
  40.  

Result

C language

     





Related Topics




Popular Posts