In mathematics, a palindrome number is defined as a number which remains the number when its digits are reversed. As the palindrome numbers carry great significance in mathematics, physics and engineering, the study of palindrome number is considered essential in all science related faculties.
Checking palindrome number is a popular tutorial of C programming language to learn the basic looping techniques. It also gives the idea of the most widely used input and output functions such as printf() and scanf() along with implementation of simple mathematical operations of C language.
The basic working principle of the C program presented here is reversion of the given number. In order to check if a number is palindrome or not, the number is initially reversed and then compared with the original number. If the number and its reverse are found to be same, the number is palindrome, else not.
- Declare and initialize the necessary variables
- Enter the number to be checked for palindrome property
- Assign the number with temporary variable
- Reverse the number the number using loop
- Store the reversed number
- Compare the reversed number and original number stored in temporary variable
- If the number and its reverse are found to be same, print the number as palindrome number
- Otherwise, the number is not palindrome
Source Code to Check Palindrome Number in C:
int num, rev = 0, temp; // declaration of variable
printf(" Enter a number: ");// Asking for number to check
temp = num; // Equalizing of temporary variable to the number
while( temp != 0 ) // loop to reverse the number
rev = rev * 10;
rev = rev + temp%10;
temp = temp/10;
if ( num == rev ) // comparison of reverse with the original number
printf(" %d is a Palindrome Number.\num", num);
printf(" %d is not a Palindrome Number.\num", num);
The above source code for checking palindrome number in C is short, simple and easy to understand. It includes a single and compulsory header file: stdio.h for using basic input and output library functions.
As this C program is executed, it asks for the number which is to be checked if it is palindrome. The number is stored in a temporary variable and then it is reversed. After that, the program compares the reverse and the original number; if they are found to be same, the number is palindrome. A sample output is shown below:
This source code in C program to Check Palindrome Number is to be compiled in Code::Blocks IDE. This tutorial is an important part and implementation of looping technique in C programming. If you have any queries regarding the any content of the post – the algorithm or source code, bring them up from the comments section.