Palindrome Program in C

 Palindrome Program in C


What is Palindrome Number?
A Palindrome number is a number which reverse is equal to the original number means number itself.
 For example : 121, 111, 1223221, etc.

In the above example you can see that 121 is a palindrome number. Because reverse of the 121 is same as 121.

Using While loop

#include<stdio.h>
#include<conio.h>
void main(){
     int n, reverse = 0, temp;
     printf("\nEnter the number : \n");
     scanf("%d",&n);
     temp = n;
     while(temp!=0){        
        reverse = reverse*10 + temp%10;        
        temp=temp/10;    
        }
     if(reverse == n){
        printf("the given number is a palindrome");
     }
     else{
        printf("the given number is not a palindrom");
     }
     getch();
 }

OUTPUT


LOGIC

Step 1: We are taking a number from the user i.e. stored in the variable n.

Step 2: We are using while loop for comparing the entered number from the reverse formula i.e.

reverse = reverse*10 + temp%10;        
        temp=temp/10;  

Step 3: Finally if condition is checked where if the reverse number is equal to the entered number then it will print the value is palindrome else it is not palindrome.


0 Comments