Before going to the program first let us understand what is a Palindrome Number?

Palindrome Number:

                                 It is a number which remains same even when the number is reversed.

For example,

                Numbers like 0,1,2,11,44,121,222,242,345543 are called as Palindrome Numbers.

Program code to find whether a Number is Palindrome or Not:

#include<iostream.h>
#include<conio.h>
void main()
{ int i,n,r,s=0; clrscr(); cout<<"n Enter The Number:"; cin>>n; for(i=n;i>0; ) { r=i%10; s=s*10+r; i=i/10; } /* CHECKING IF THE NUMBER ENTERED AND THE REVERSE NUMBER IS EQUAL OR NOT */ if(s==n) { cout<<n<<" is a Palindrome Number"; } else { cout<<n<<" is a not Palindrome Number"; } getch();
}

Related: C program for Palindrome number using For Loop

Working:

  • First the computer reads the number to find the reverse of the number.
  • Then using for loop the reverse number is calculated.
  • Then finally the reverse number is compared with the entered number and if they are equal then it is printed as “(Given number) is a palindrome number ” else it is printed as “(Given number) is not a palindrome number “.

Step by Step working of the above Program Code:

Let us assume that the number entered by the user is 121.

  1. It assigns the value of s=0,n=121.
  2. Then it assigns the value of i=n (i=121) in the initialization part of the for loop.
  3. Then the loop continues till the condition of the for loop is true.

3.1.    i>0  (121>0)  for loop condition is true

r=i%10        (r=121%10)      So  r=1

s=s*10+r     (s=0*10+1)        So  s=1

i=i/10          (i=121/10)        So  i=12

3.2.    i>0  (12>0)  for loop condition is true

r=i%10        (r=12%10)      So  r=2

s=s*10+r    (s=1*10+2)        So  s=12

i=i/10          (i=12/10)        So  i=1

3.3.    i>0  (1>0)  for loop condition is true

r=i%10        (r=1%10)         So  r=1

s=s*10+r    (s=12*10+1)      So  s=121

i=i/10          (i=1/10)          So  i=0

3.4.    i>0  (0>0)  for loop condition is false

It comes out of the for loop and checks whether the number is palindrome or not.

  1. s==n   (121==121)   if condition is true

It prints 121 is a palindrome number.

  1. Thus the program execution is completed.

In the above working, step:4 if condition can also be false for some other number like ’12’.

Then for that it goes to the else part and prints 12 is not a palindrome number.

Output:

palindrome number
palindrome number

TO DOWNLOAD THE PROGRAM CODE : CLICK HERE

Similar Posts