C program for Palindrome Number using While Loop
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,55,131,262,464,453354 are called as Palindrome Numbers.
Program code to find whether a Number is Palindrome or Not:
#include<stdio.h> #include<conio.h> void main() { int n,a,r,s=0; clrscr(); printf("\n Enter The Number:"); scanf("%d",&n); a=n; //LOOP TO FIND REVERSE OF A NUMBER while(n>0) { r=n%10; s=s*10+r; n=n/10; } /* CHECKING IF THE NUMBER ENTERED AND THE REVERSE NUMBER IS EQUAL OR NOT */ if(a==s) { printf("\n %d is a Palindrome Number",a); } else { printf("\n %d is not a Palindrome Number",a); } getch(); }
Related: C++ program for Palindrome Number using while Loop
Working:
- First the computer reads the number to find the reverse of the number.
- Then using while 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.
- It assigns the value of s=0,n=121.
- Then it assigns the value of a=n (a=121).
- Then the loop continues till the condition of the while loop is true.
3.1. n>0 (121>0) while loop condition is true
r=n%10 (r=121%10) So r=1
s=s*10+r (s=0*10+1) So s=1
n=n/10 (n=121/10) So n=12
3.2. n>0 (12>0) while loop condition is true
r=n%10 (r=12%10) So r=2
s=s*10+r (s=1*10+2) So s=12
n=n/10 (n=12/10) So n=1
3.3. n>0 (1>0) while loop condition is true
r=n%10 (r=1%10) So r=1
s=s*10+r (s=12*10+1) So s=121
n=n/10 (n=1/10) So n=0
3.4. n>0 (0>0) while loop condition is false
It comes out of the while loop and checks whether the number is palindrome or not.
- a==s (121==121) if condition is true
It prints 121 is a palindrome number.
- 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:
TO DOWNLOAD THE PROGRAM CODE :CLICK HERE
very nice thank you so much 😊 😀