2

#include <iostream>
using namespace std;

int main() {
    int n;
    int reversedNumber = 0;
    int remainder;

    cout << "Enter an integer: ";
    cin >> n;

    while (n != 0) {
        remainder = n % 10;
        reversedNumber = (reversedNumber * 10) + remainder;
        n /= 10;
    }
    if (reversedNumber == n)
        cout << "YES";
    else
        cout << "NO";
    return 0;
}

Hello, I want the compiler to show yes but when i enter 2356532 in my input shows No ,This program should show that the input is equal to the inverse number.`

Gdos
  • 23
  • 5
  • 5
    use the debugger to see what happens – rioV8 Mar 10 '21 at 20:55
  • 1
    Write a function `int reversedNumber( int n )`, test it then check condition by `if( reversedNumebr( n ) == n )...` – Slava Mar 10 '21 at 20:58
  • 1
    If you don't have a good IDE with a debugger and have code like this you could print the numbers to easily figure out what is happening. With that said there are many good and free IDEs with debugging. It will greatly help you to learn how to debug. – drescherjm Mar 10 '21 at 20:59
  • 1
    [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Gary Strivin' Mar 10 '21 at 21:02

2 Answers2

5

You divide n /= 10 in your loop until you have 0 left so if (reversedNumber == n) will never be true for anything but 0 as input.

Save n before the loop and compare with the value you've saved after the loop.

Example:

int saved = n;
while (n != 0) {
    remainder = n % 10;
    reversedNumber = (reversedNumber * 10) + remainder;
    n /= 10;
}
if (reversedNumber == saved) ...

Demo

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
0

You are clearly updating the value of n inside the while loop. At the end, its value will always be 0. Hence, your if statement will always return false.
I suggest you save the initial value of n in a new variable after getting user input and make the comparison between the reversedNumber and that value.

dev1ce
  • 101
  • 3
  • 11