Today we will learn the Palindrome program in C and Palindrome String program in C.So, Before start learning, you should have knowledge of palindrome.
Table of Contents
What is Palindrome?
The number or string is said to be palindrome if the reverse of that number or string is the original number or string itself.
For example: ‘121’ and ‘madam’
In the above examples, we can observe that if we reverse the number 121 and string madam we will get the same number and string. So, the number and the given string is called Palindrome.
There are so many Program methods to get the Palindrome of number and string we will see it one by one.
1. Palindrome program Using while loop
In this program we will ask user to input a number which user wants to check whether the given number is palindrome or not.
#include <stdio.h> int main() { int n, reversedN = 0, remainder, originalN; printf("Enter an integer: "); scanf("%d", &n); originalN = n; // reversed integer is stored in reversedN while (n != 0) { remainder = n % 10; reversedN = reversedN * 10 + remainder; n /= 10; } // palindrome if orignalN and reversedN are equal if (originalN == reversedN) printf("%d is a palindrome.", originalN); else printf("%d is not a palindrome.", originalN); return 0; }
Output:
Here the user enters the number and the number is stored in the variable n. Then the number is assigned to the variable originalN and the reverse variable is stored in the reversedN.
If the reversedN is equal to originalN then the given number is Palindrome otherwise, the number is not Palindrome.
2. Program to check given String is Palindrome
In this program we will put some string in the function then the program will check whether the string is Palindrome or not.
#include <stdio.h> #include <string.h> // A function to check if a string str is palindrome void isPalindrome(char str[]) { // Start from leftmost and rightmost corners of str int l = 0; int h = strlen(str) - 1; // Keep comparing characters while they are same while (h > l) { if (str[l++] != str[h--]) { printf("\n%s is Not Palindrome", str); return; } } printf("\n%s is palindrome", str); } // Driver program to test above function int main() { isPalindrome("abb"); isPalindrome("abbccbba"); isPalindrome("madam"); return 0; }
Output:
3. Check Palindrome Using Recursion
In this program we will check whether the given number is palindrome or not in C using Recursion Concept.
#include <stdio.h> int Reverse_Integer (int); int main() { int Number, Reverse = 0; printf("\nPlease Enter any number to Check for Palindrome\n"); scanf("%d", & Number); Reverse = Reverse_Integer (Number); printf("Reverse of entered number is = %d\n", Reverse); if ( Number == Reverse ) printf("\n%d is Palindrome Number.\n", Number); else printf("%d is not the Palindrome Number.\n", Number); return 0; } int Reverse_Integer (int Number) { static Reminder, Reverse = 0; if (Number > 0) { Reminder = Number %10; Reverse = Reverse *10+ Reminder; Reverse_Integer (Number /10); return Reverse; } else return 0; }
Output:
In recursion the function calls itself again and again.In above program while executing when the compiler will reach to the Reverse_Integer, then the compiler will jump to Reverse_Integer function.
If(Number>0) checks whether the number is 0 or not.It is essential to declare before recursive function otherwise the program will start executing infinite number of times.
4. Recursion Using For loop
In this program we will use for loop instead of while loop. Don’t be afraid the logic is same only the loop is changed.
#include <stdio.h> int main() { int Number, Temp, Reminder, Reverse = 0; printf("\nPlease Enter any number to Check for Palindrome\n"); scanf("%d", &Number); for(Temp = Number; Temp > 0; Temp = Temp /10 ) { Reminder = Temp %10; Reverse = Reverse *10+ Reminder; } printf("Reverse of entered number is = %d\n", Reverse); if ( Number == Reverse ) printf("\n%d is Palindrome Number.\n", Number); else printf("%d is not the Palindrome Number.\n", Number); return 0; }
Output:
In above program when the user enters the number, the program first reverse the number and then check whether the number is palindrome or not.
5. Palindrome Using Functions
In this program we will use function to check whether the entered number is Palindrome or not.The function Reverse_Integer is used for checking.
#include <stdio.h> int Reverse_Integer (int); int main() { int Number, Reverse = 0; printf("\nPlease Enter any number to Check for Palindrome\n"); scanf("%d", & Number); Reverse = Reverse_Integer (Number); printf("Reverse of entered number is = %d\n", Reverse); if ( Number == Reverse ) printf("\n%d is Palindrome Number.\n", Number); else printf("%d is not the Palindrome Number.\n", Number); return 0; } int Reverse_Integer (int Number) { int Reminder, Reverse = 0; for(; Number > 0; Number = Number /10 ) { Reminder = Number %10; Reverse = Reverse * 10 + Reminder; } return Reverse; }
Output:
6. Program to find Palindrome numbers between 1 to 1000
This program is very excellent. In this program we have to enter the minimum and maximum three digit number. Then, the program will print all the palindrome numbers between minimum and maximum number.
#include<stdio.h> int main() { int Number,Reminder,Reverse,Temp; int Minimum,Maximum; printf("\nPlease Enter the Minimum & Maximum Values\n"); scanf("%d %d",&Minimum, &Maximum); printf("Palindrome numbers Between %d and %d are:\n",Minimum, Maximum); for(Number=Minimum;Number<=Maximum;Number++) { Temp=Number; Reverse =0; while (Temp > 0) { Reminder = Temp %10; Reverse = Reverse *10+ Reminder; Temp = Temp /10; } if(Number==Reverse) printf("%d ",Number); } return 0; }
Output:
In this program the for loop restrict the compiler to repeat the iterations between maximum and minimum number. And thus prints all the Palindrome number between them. Only if Number==Reverse
Also Read:
- C Program to print Prime Numbers from 1 to n
- C Program to Make Simple Calculator
- Armstrong Number Program in C
- Fibonacci Series Program in C
- Decimal to Binary Conversion Program in C
- Reverse a String in C
- Program to Reverse a Number in C
- Hello World Program in C
- Leap Year Program in C
- Factorial Program in C
- Prime Number Program in C