C Program to Check Whether a Number is a Palindrome or Not
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 codeint 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)


