Home C Programming Language / Programs / C Program to Check whether the Given Number is a Palindromic
🚀 Programming Example

C Program to Check whether the Given Number is a Palindromic

👁 1,766 Views
💻 Practical Program
📘 Step Learning

If a number, which when read in both forward and backward way is same, then such a number is called a palindrome number.

 

Example:
121, 23532, 12321 etc.

💻 Program Code

#include <stdio.h>

int main() {
    int n, n1, rev = 0, rem;
    
    printf("Enter any number: ");
    scanf("%d", &n);    
    n1 = n;
    
    /* logic */    while (n > 0){
        rem = n % 10;
        rev = rev * 10 + rem;
        n = n / 10;
    }
    
    if (n1 == rev){
        printf("Given number is a palindromic number \n"); 
    }
    else{
        printf("Given number is not a palindromic number \n"); 
    }    
    return 0; 
} 
                        

🖥 Program Output

<b> Output 1</b>
Enter any number: 1221
Given number is a palindromic number
Press any key to continue . . .

<b> Output 2</b>
Enter any number: 1232
Given number is not a palindromic number
Press any key to continue . . .
                            

📘 Explanation

Consider a numbern=121, reverse=0 and remainder;
number=121

now the while loop is executed /* the condition (n>0) is satisfied */

/* calculate remainder */
remainder of 121 divided by 10=(121%10)=1;
now reverse=(reverse*10)+remainder
=(0*10)+1 /* we have initialized reverse=0 */
=1
number=number/10
=121/10
=12

now the number is 12, greater than 0. The above process is repeated for number=12.
remainder=12%10=2;
reverse=(1*10)+2=12;
number=12/10=1;
now the number is 1, greater than 0. The above process is repeated for number=1.
remainder=1%10=1;
reverse=(12*10)+1=121;
number=1/10 /* the condition n>0 is not satisfied,control leaves the while loop */

Program stops here. The given number=121 equals the reverse of the number. Thus the given number is a palindrome number.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.