Write C++ program to find reverse of a number using recursion


I have used CodeBlocks 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

Introduction

I have used CodeBlocks compiler for debugging purpose. But you can use any C++ programming language compiler as per your availability.


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

Result

C++ language techstudy.org

     





Related Topics




Popular Posts