Writing a C program to check whether a number is palindrome or not is a beginner-level yet essential programming task. A palindrome number is a number that remains the same when its digits are reversed. For Example-121, 131, 1331, etc are all palindrome numbers because they read the same from left to right as well as right to left.
Table of Contents
Problem Statement
Write a C program to check whether a number is palindrome or not.
C Program to Check Palindrome Number
Let’s write a C program that checks whether the given number is a palindrome or not.
#include <stdio.h>
int isPalindrome(int number)
{
int orignal = number;
int reverse = 0;
int remainder;
while (number > 0)
{
remainder = number % 10;
reverse = reverse * 10 + remainder;
number = number / 10;
}
if (orignal == reverse)
{
return 1;
}
else
{
return 0;
}
}
void main()
{
int num;
printf("Enter the number ");
scanf("%d", &num);
if (isPalindrome(num))
{
printf("The given number %d is a Palindrome number ", num);
}
else
{
printf("The given number %d is not a Palindrome number ", num);
}
}
Output
How the Above C Program Works
- The
palindrome
function takes an integer as an argument and returns 1 if the number is palindrome otherwise it returns 0. - Inside the palindrome function, we store the original number in the original, and a variable reverse is initiated to store the reversed number.
- The while loop is used to reverse the digits of the number by extracting the remainder when divided by 10 and building the reverse number.
- After reversing the number if the original number and reverse number are equal then the isPalindrome function returns 1(true) otherwise it returns 0(false).
- Inside the main method, isPalindrome function is called, and based on its return value the program prints whether the given number is palindrome or not.
The concept used in this article to check whether a number is palindrome or not can be implemented in all other programming languages like C++, Java, Python, Go, etc.
Happy Coding
1 thought on “C Program to Check Whether a Number is Palindrome or not”