A Program to Reverse an Integer
In this example, we are giving a program to reverse an integer given by user.
Program to reverse an integer:
#include <iostream>
using namespace std;
int main()
{
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
{
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
}
cout << "Reversed Number = " << reversedNumber;
return 0;
}
Output:
Enter an integer: 1234 Reversed Number = 4321