C++ Program to Reverse Number using For Loop

Write C++ Program to Reverse Number using For Loop

// CPP Program to Reverse Number using For Loop

#include <iostream>

using namespace std;

int main()
{
	long int n, ans = 0;

	cout << "Enter the number:--> ";
	cin >> n;

	for(; n!=0; )
	{
		int remainder = n % 10;  //2  //2  //4
		ans = ans*10 + remainder;  //2  //22  //224
		n = n / 10;   //42  //4
	}
	cout << "The answer is " << ans;

	return 0;
}

Output:

Enter the number:--> 456
The answer is 654

≪ Previous | Next ≫