Take the last digit with % 10, add it to the result, then drop it with / 10.
int n = 1234, rev = 0;
while(n != 0){
int digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}
cout << rev; // 4321
Take the last digit with % 10, add it to the result, then drop it with / 10.
int n = 1234, rev = 0;
while(n != 0){
int digit = n % 10;
rev = rev * 10 + digit;
n /= 10;
}
cout << rev; // 4321