We can invert a numerical value in C++ by employing loops and mathematical operators. Within this code, we receive a numerical input from the user and proceed to reverse this value.
Let's examine a basic C++ illustration demonstrating the reversal of a specified number.
Example
Example
#include <iostream>
using namespace std;
int main()
{
int n, reverse=0, rem;
cout<<"Enter a number: ";
cin>>n;
while(n!=0)
{
rem=n%10;
reverse=reverse*10+rem;
n/=10;
}
cout<<"Reversed Number: "<<reverse<<endl;
return 0;
}
Output:
Output
Enter a number: 234
Reversed Number: 432