a program that flips integers

I was asked to write a program that prompts the user to enter a 3-digit number, the program flips & displays the number.
for example: input (286) .. output (682)
input (690)... output (96)---> without the (0)

my program works well when inputting an integer that doesn't have a zero at the end of it (e.g. 286) but when I input (690) it displays (096) and not (96).

what should I add to my code in order to prevent the program from displaying the 0.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream>
using namespace std;
int main()
{
   int n1, rn1, n2, n3, num;
   cout<<"Enter a 3-digits postive integer: ";

   cin>> num;

   n1 = num/100;
   rn1 = num%100;
   n2 = rn1/10;
   n3 = rn1%10;

   cout<<"The flipped value of "<<num<<" is "<<n3<<n2<<n1;

   return 0;
}
Last edited on
Use the same math you already know to recombine the digits back into a number again - then just print that number instead of the individual digits.
n1 = num/100;
rn1 = num%100;
n2 = rn1/10;
n3 = rn1%10;
flip = n3+n2+rn1+n1;

cout<<"The flipped value of "<<num<<" is "<<flip;

It didn't work.
It displayed 2 digits, but they were incorrect, I entered 268 & it displayed 84.
Thank you though!
Last edited on
Your math is wrong. Don't forget you need to multiply each digit by the place you want it to be in. For example, to get a digit in the hundreds place you need to multiply it by 100.
Last edited on
Topic archived. No new replies allowed.