Open In App

Palindrome Number Program in C

Palindrome numbers are those numbers which after reversing the digits equals the original number. For example, 12321 after reversing is 12321, so it is a palindrome number. But the number 1232 after reversing is 2321, so it is not a palindrome number. In this article, we will look at the C program to check whether a number is a palindrome or not.



Algorithm

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.

  1. Declare an integer reversed = 0 and num = original_number
  2. Now to reverse the number, do the following while num >= 0
    1. reverse = 10 * reverse + num % 10;
    2. num /= 10;
  3. After getting the reverse number, compare it with the original _number,
  4. If the reverse is equal to the original_number, then the number is palindrome.
  5. Else, the number is not palindrome.

Palindrome Number Program in C




// C program to check whether a number is palindrome or not
#include <stdio.h>
  
// Driver code
int main()
{
    // 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

Complexity Analysis

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


Article Tags :