A number is said to be palindrome if it satisfy’s a condition,the reverse of the number should be same as the original number.For finding the reverse of the given number we need to get the individual digits of given number as we have done in finding the sum of digits of a given number.I shall explain it again.
To get the last digit of a number, we need to divide the given number with 10 and remainder which we get as a result is the last digit of the number.If the given number is 789 and if we divide the number with 10 we get 9 as remainder and remaining two digits 78 as quotient.If we divide the number 78 by 10 then we get 8 as remainder and 7 as quotient.By dividing 7 by 10 we get 7 as remainder and 0 as quotient.So we have got all the digits of the given number.
By using the same logic in our program we are going to check for palindrome program.In the program we are using the variable n for storing the input,remainder for storing the remainder after each division of number with 10 and reverse for finding the reverse of the number.
#include<stdio.h> #include<conio.h> void main() { int n,remainder,rev=0,temp; clrscr(); printf("Enter a number to check whether palindrome or not:"); scanf("%d",&n); temp = n; while(temp>0) { remainder = temp%10; temp = temp/10; rev=rev*10+remainder; } printf("Reverse value is :%d",rev); if(rev == n) printf("\nGiven number %d is palindrome",n); else printf("Given number %d is not palindrome",n); getch(); }
Output:
Download the source code
Pingback: Palindrome program in Python | Letusprogram