Program to calculate reverse of a number in C++.
Solution:
#include <iostream>
using namespace std;
int rev(int n)
{
static int r=0;
if(n==0)
return r;
else
{
r=(r*10)+n%10;
return rev(n/10);
}
}
int main()
{
int n,ans;
cout<<"Enter a number ";
cin>>n;
ans = rev(n);
cout << "Reverse of "<< n<<" is "<<ans;
return 0;
}
Output :
Enter a number 123 Reverse of 123 is 321