In mathematics, a palindrome number is defined as a number that remains the number when its digits are reversed. As the palindrome numbers carry great significance in mathematics, physics, and engineering, the study of palindrome numbers is considered essential in all science-related faculties.
Checking Palindrome Program in C is a popular tutorial in 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 the implementation of simple mathematical operations in C language.
The basic working principle of the C program presented here is the reversion of the given number. In order to check if a number is a 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 the same, the number is a palindrome program in C, else not.
Algorithm:
- Start
- Declare and initialize the necessary variables
- Enter the number to be checked for palindrome property
- Assign the number with a temporary variable
- Reverse the number using loop
- Store the reversed number
- Compare the reversed number and original number stored in a temporary variable
- If the number and its reverse are found to be the same, print the number as a palindrome number
- Otherwise, the number is not a palindrome
- Stop
Source Code to Check Palindrome Program in C (Number):
#include <stdio.h>
int main()
{
int num, rev = 0, temp; // declaration of variable
printf(" Enter a number: ");// Asking for number to check
scanf("%d",&num);
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);
else
printf(" %d is not a Palindrome Number.\num", num);
return 0;
}
The above source code for checking palindrome numbers 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 a 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 the same, the number is a palindrome. Sample output is shown below:
This source code in C program to Check the Palindrome Program in C (Number) is to be compiled in Code::Blocks IDE. This tutorial is an important part and implementation of the looping technique in C programming. If you have any queries regarding any content of the post – the algorithm or source code, bring them up from the comments section.
What is Palindrome?
A palindrome is a word or phrase that reads the same both when read forward and when read backward.
Example:
The name of the author of this article is Ankit Soni.
Ankit Soni
Is it a palindrome? Yes, because it reads the same backwards as forwards:
KaniSoni
In the example above, if the string is of length more than 1, then the palindrome becomes palindromic. The following code snippet can be used to find palindromes:
#include
#include
using namespace std;
int main()
{
string s;
cin >> s;
char *p = s.c_str();
while (*p)
{
if (*p == *(--p))
break;
}
cout << "Palindrome is " << s << '\n';
return 0;
}
Output: palindrome is Ankit Soni
The next program is used to find palindromes in a given string. It uses the method explained above to find palindromes in a string.
#include
#include
using namespace std;
int main()
{
string s;
cin >> s;
char *p = s.c_str();
while (*p)
{
if (*p == *(--p))
break;
}
if (s.size() % 2 == 0)
cout << "Palindrome is " << s << '\n';
return 0;
}
Output: palindrome is Ankit Soni
The next program is a palindrome checker.
#include
#include
#include
using namespace std;
int main()
{
vector v;
string s;
int i = 0;
for (;;)
{
cin >> s;
if (v.size() <= i)
v.push_back(s);
else
v[i].push_back(s);
if (s.size() % 2 == 0)
cout << s << endl;
else
cout << "Not a palindrome\n";
++i;
}
return 0;
}
Output: Ankit Soni
palindrome is Ankit Soni