reverse a number using recursion in c 2b 2b

Solutions on MaxInterview for reverse a number using recursion in c 2b 2b by the best coders in the world

showing results for - "reverse a number using recursion in c 2b 2b"
Iris
11 Nov 2020
1#include <iostream>
2#include <math.h>
3 
4using namespace std;
5 
6 //Fuction declaration
7int reverseNumber(int num);
8 
9int 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 
27int 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}