The Wayback Machine - https://web.archive.org/web/20220906155309/https://www.geeksforgeeks.org/c-program-to-check-whether-a-number-is-a-palindrome-or-not/
Skip to content
Related Articles

Related Articles

C Program to Check Whether a Number is a Palindrome or Not

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Aug, 2022
View Discussion
Improve Article
Save Article

Here, we will see how to check whether a number is a palindrome or not using a C program. Below are the examples:

Input: Number = 12321 
Output: Given number is a Palindrome number.

Input: Number = 1232
Output: Given number is not a Palindrome number.

Approach: A simple method for this problem is to first reverse all the digits of a given number and then compare the reverse of the number with a given number. If both are the same, then return true, else false. 

Below is the C program to check whether a number is a palindrome or not:

C




// C program to check whether
// a number is palindrome or not
#include <stdio.h>
 
// Driver code
int main()
{
    // Define variables
   
    // This is our given number
    int original_number = 12321;
   
    // This variable stored reversed digit
    int reversed = 0;
   
    int num = original_number;
 
    // Execute a while loop to reverse
    // digits of given number
    while (num != 0)
    {
        int r = num % 10;
        reversed = reversed * 10 + r;
        num /= 10;
    }
 
    // Compare original_number with
    // reversed number
    if (original_number == reversed)
    {
        printf(" Given number %d is a palindrome number",
                 original_number);
    }
    else
    {
        printf(" Given number %d is not a palindrome number",
                 original_number);
    }
    return 0;
}

Output

 Given number 12321 is a palindrome number

Time Complexity: O(D) where D is the count of digits of the given number.
Auxiliary Space: O(1) 

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!