Logic:
- Palindrome: A number is palindrome if it is equal to its reverse.
- Enter the number n
- Example 1: n=345
- Reverse=543
- Number is not equal to its reverse.
- So it is not palindrome
- Example 2: n=121
- Reverse=121
- Number is equal to its reverse.
- So it is palindrome
Algorithm
- Enter the number as n
- Copy the number n into a temporary variable, say, no
- First find the reverse of the number
- Initially assign rev=0
- while(n>0)
- {
- r=n%10;
- rev = rev * 10 + r;
- n=n/10;
- }
- if (rev==no)
- printf(“It is palindrome”);
- else
- printf(“It is not palindrome”);
Program
#include<stdio.h> int main() { printf("****************************************************************"); printf("\n****************************************************************"); printf("\n** WAP to check a number for palindrome **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************************"); printf("\n****************************************************************\n"); int i,n,r,rev=0,no; printf("Enter the no n \n"); scanf("%d",&n); no=n while(n>0) { r=n%10; n=n/10; rev=rev*10+r; } if(no==rev) printf("Number is palindrome"); else printf("Number is not palindrome"); }
Output 1
*************************************************************************** *************************************************************************** ** WAP to check a number for palindrome ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************************** *************************************************************************** Enter the no n 345 Number is not palindrome
Output 2
*************************************************************************** *************************************************************************** ** WAP to check a number for palindrome ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************************** *************************************************************************** Enter the no n 121 Number is palindrome