C++ Program to Reverse Number using Recursion

Write C++ Program to Reverse Number using Recursion

// CPP Program to Reverse Number using Recursion

#include <iostream>

using namespace std;

int reverse_number(int n)
{
	static long int ans = 0;
	if(n <= 0)
	{
		return ans;
	}
	int remainder = n % 10;
	ans = ans * 10 + remainder;

	reverse_number(n / 10);

	return ans;

}

int main()
{
	long int n;
	cout << "Enter Number :--> ";
	cin >> n;

	cout << "Reverse of " << n << " = " << reverse_number(n);

	return 0;
}

Output:

Enter Number :--> 123456
Reverse of 123456 = 654321

≪ Previous | Next ≫