#include <iostream>

using namespace std;

class Reverse
{
  int n,num,dig,rev;
  public:
  void Rev()
  {
    cout<<"enter an integer number to check if it palindrome or not \t";
    cin>>num;
    n=num; //
    rev=0;
    while(num>0)
   {
    dig=num%10; // 48%10=4
      rev= rev*10+dig; // rev=48*10+4=484
      num=num/10;  //484/10 =48
    cout<<num;
   }
   if (n==rev)
   cout<<"given number is palindrome";
   else
   cout<<"given number is not palindrome";
}
};
int main()
{
    Reverse P;
    P.Rev();
    return 0;
}
dig=num%10; // 48%10=4
rev= rev*10+dig; // rev=48*10+4=484
num=num/10;  //484/10 =48

im not getting the logic here, if rev=0 is assigned, then wouldn't rev= rev*10+dig be equal to dig? :icon_frown:

dig=num%10; // 48%10=4
rev= rev*10+dig; // rev=48*10+4=484
num=num/10;  //484/10 =48

First things first, 48%10 is not equal to 4, its 8. Modulus operator gives the remainder of the division.
And the in first loop rev=0*10+8=8 not rev=48*10+4=484, got it?

Now you try again. Give a value to the num. And you will get the logic. The thing is that logic is something you have to develop, and best way is to try putting values for the input variable, and doing the program on paper.

im not getting the logic here, if rev=0 is assigned, then wouldn't rev= rev*10+dig be equal to dig? :icon_frown:

Off course yes, see this is not a sequential program, there is iteration (loops) involved, for the first loop, rev maybe equal to dig, but see, in next loop rev is not equal to 0, and it goes on until num<=0.

Now does it ring any bells? Feel free to ask, I think i can help you with this.....

i tried it with 121 this time still not getting how this thing working :'(

dig=num%10; //  dig=12%10=2
rev= rev*10+dig; // rev=0*10+2=2
num=num/10;  //   num=121/10=12
 cout<<num;

Off course yes, see this is not a sequential program, there is iteration (loops) involved, for the first loop, rev maybe equal to dig, but see, in next loop rev is not equal to 0, and it goes on until num<=0.

rev value isnt getting incremented or decremented here, so how is its value changing dude ? :(

in next loop rev is not equal to 0, and it goes on until num<=0.

also it is num>0 in the code.

Oh boy, The fact is that this program uses iteration, meaning loops. If you don't know loops yet, click here

Anyway i will tell you how the loop work in case you enter 121 as the input.

First loop

num=121
dig=121%10=1
rev=0*10+1=1
num=121/10=12

But see, num>0, so the loop don't stop there, it goes for another round(It will stop only when num<=0), here value are taken from the previous loop.

So the second loop is like this

num=12
dig=12%10=2
rev=1*10+2
num=12/10=1

Still num>0, so again loop is executed..

so in third loop

num=1
dig=1%10=1
rev=12*10+1=121
num=1/10=0

Here num=0, so the loop stops because the loop works as long as num>0. see

while(num>0)

Now rev is equated with n, if they are equal, then they are palindrome, else not.
Understand...

Got the logic now?

Comments
explained while loop clearly.

rev value isnt getting incremented or decremented here, so how is its value changing dude ? :(

The rev value is changing because it is getting reassigned each time the loop is run (Unless you have specified the value of the variable to never change like:

const int rev;

eg. to make it easier for you to understand

int i=0;
cin>>i; //you input 5, now the value of i ie 0 get replaced with 5
cout<<i; //The result will be 5 here
This question has already been answered. Start a new discussion instead.