C++ Program to Reverse the Number using While Loop

Write C++ Program to Reverse the Number using While Loop

// CPP Program to Reverse the Number using While Loop

#include <iostream>

using namespace std;

int main()
{
	long int n, ans = 0;
	cout << "Enter the number:--> ";
	cin >> n;

	while(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 ≫