I wrote a small C ++ program that asks the user for input, the user gives the number, then the computer displays the number with the opposite sign.
For example: 17 becomes 71. 123 becomes 321.
This program:
#include <iostream>
#include <string> //for later use.
using namespace std;
int rev(int x)
{
int r = 0;
while(x)
{
r = (r*10) + (x%10);
x = x/10;
}
return r;
}
int main()
{
int nr;
cout << "Give a number: ";
cin >> nr;
rev(nr);
cout << nr;
return 0;
}
The end result of the program: prints the same number, the function does not work. What am I doing wrong? I tried several solutions, but to no avail.
source
share