Updated March 16, 2023
Introduction to Palindrome in C Program
Palindrome means reversing a given object that gives the same result as original. Object can be anything, a number, a string, phrase, etc. In this article, we are going to see how to see what is palindrome in C, How it works in the C program, how to identify if the given number or string is a palindrome or not. As we discussed earlier, palindrome is the same result that you get after reversing. For example, 121 if you reverse a given number 121, the result will be the same i.e. 121. So we can say that 121 is a palindrome.
Below are some examples to identify whether they are palindrome are not
- 12321 – Reverse is 12321, so it is a palindrome.
- 12342 – Reverse is 24321, so it is not a palindrome.
- abcdcba – Reverse is abcdcba, so it is a palindrome.
- acbac – Reverse is cabca, so it is not a palindrome.
Form the above example, we can now identify whether it is a palindrome or not. This is what we have done is theoretically or just orally. Now we are going to see how we can check palindrome in C program. Before moving to that, first, we will see the logic of palindrome in C.
The logic of palindrome in C program is given below:
- Get an input form the user.
- Store the input in a temporary variable.
- Find the reverse of the input entered by the user.
- Compare the reverse of input with the temporary variable.
- If both reverse and temporary variables, matches, print it is a palindrome.
- If both reverse and temporary variables do not match, print it is not a palindrome.
How to Check the Palindrome in C Program?
In this section, we are going to see how to check palindrome in C using various methods with the help of examples.
Example #1 – Using while loop
Working of While Loop is given as below:
While loop gets executed several times based on the condition which is mentioned after the word While in code.
If this condition is true then it will execute the code inside the parenthesis of the While loop. If the condition is false then it will jump to the code after the While loop without executing the code of While loop. Let’s see how to check if a number is a palindrome or not using a While loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, reverse_number = 0, temp_number;
printf("Enter a number to check palindrome value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
temp_number = number; //store number to temp_number
while (number != 0)
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}
if(temp_number == reverse_number) // check reverse number with original number
{
printf("Given number is palindrome"); //if match, print palindrome
}
else
{
printf("Given number is not palindrome"); // If it don’t match with original print not palindrome
}
return 0;
}
Output:
Example #2 – Using For loop
First, we initialize and declare variables for the code. After that, the condition is evaluated. This initialization step is executed only once in the Code.
If the condition is true then it will execute the code inside the block of For loop. If the condition is false then it will jump to the code after the For loop without executing the code of For loop.
After the For loop, the increment statement will be executed. After that again the condition will be checked. Loop will get executed if the condition is true and the loop will repeat itself i.e. a body of the loop, an increment statement, and condition. The For loop ends when the condition is false. Let’s see how to check if a number is a palindrome or not using For loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, remainder_number, reverse_number= 0, i, temp_number;
printf("Enter a number to check palindrome value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
temp_number = number; //store number to temp_number
for(i = number; i >0; )
{
remainder_number= i % 10;
reverse_number = remainder_number + reverse_number * 10;
i = i/ 10;
}
if(temp_number == reverse_number) // check reverse number with original number
{
printf("Given number is palindrome"); //if match, print palindrome
}
else
{
printf("Given number is not palindrome"); // If it don’t match with original print not palindrome
}
return 0;
}
Output:
Example #3 – Using a do-while loop
A do-while loop is a kind of similar to a while loop, But in the do-while loop, the loop gets executed at least one time.
In the Do While loop, The condition appears at the end of the loop, so the statements in the Do loop executed before checking whether a condition is true or false.
If the condition is true, the loop will go back up to do, and the statements in the loop will get executed again. This process repeats again and again and terminates when the given condition becomes false.
Let’s see how to check if a number is a palindrome or not using a While loop.
Code:
#include<stdio.h>
#include<conio.h>
int main()
{
int number, reverse_number = 0, temp_number;
printf("Enter a number to check palindrome value:"); // allow user to enter a number
scanf("%d", &number); // takes value from user
temp_number = number; //store number to temp_number
do
{
reverse_number = reverse_number * 10;
reverse_number = reverse_number + number % 10;
number = number / 10;
}while(number != 0);
if(temp_number == reverse_number) // check reverse number with original number
{
printf("Given number is palindrome"); //if match, print palindrome
}
else
{
printf("Given number is not palindrome"); // If it don’t match with original print not palindrome
}
return 0;
}
Output:
Example #4 – Check String is Palindrome or not
Let’s see how to check if a string is a palindrome or not.
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char string[20];
int i, len, flag = 0;
printf("Enter a string to check palindrome:"); // allow user to enter string
scanf("%s", string); // takes string as a input
len = strlen(string); //counts string length
for(i = 0; i < len; i++)
{
if(string[i] != string[len-i-1])
{
flag = 1;
break;
}
}
if(flag)
{
printf("Given string is not palindrome");
}
else {
printf("Given string is palindrome");
}
return 0;
}
Output:
Conclusion
In this article, we have seen how to check if a number and string is a palindrome or not in C with some examples along with elaborated examples. I hope this article will help you in understanding the working of Palindrome number and you find this article helpful.
Recommended Articles
This is a guide to Palindrome in C Program. Here we discuss C program to check whether the number or string is Palindrome or not with some examples. You can also go through our other suggested articles to learn more –