Write a C Program to Check Whether the Number is Palindrome or Not using while Loop, For Loop, Functions, and Recursion. We also show you how to write a C Program for Palindrome Numbers from 1 to 1000.
Any number could be Palindrome if it remained the same when we reversed it. For example, 191 because it remains the same after reversing. The following steps are the standard procedure to check for the Palindrome Number in C Programming.
- Please enter any value.
- Reverse a given number and compare it with the original given value.
- If they matched correctly, then it is a Palindrome number. Otherwise, it is not.
C Program to Find Palindrome Number While Loop
It allows the user to enter any positive integer. Next, this C program checks whether the given number is Palindrome or Not using the While Loop.
#include <stdio.h> int main() { int Number, Temp, Reminder, Rev = 0; printf("\nPlease Enter any value \n"); scanf("%d", &Number); //Helps to prevent altering the original value Temp = Number; while ( Temp > 0) { Reminder = Temp %10; Rev = Rev *10+ Reminder; Temp = Temp /10; } printf("Rev of entered = %d\n", Rev); if ( Number == Rev ) printf("\n%d is Palindrome.\n", Number); else printf("%d is not.\n", Number); return 0; }
In this C Palindrome Program, We assign the original number to the Temp variable. It helps to protect our original value and perform the manipulation on the Temp variable.
Next, the While Loop Condition will make sure that the given num is greater than 0
User Entered values : Number= 1331 and Rev = 0
Temp = Number
It means Temp = 1331
First Iteration of Palindrome Program in C
Reminder = Temp %10
Reminder = 1331%10 = 1
Rev = Rev *10 + Reminder
Rev = 0 * 10 + 1 = 0 + 1 => 1
Temp = Temp /10 => 1331 /10
Temp = 133
Second Iteration: From the first Iteration of the palindrome number program in c, the values of both have changed as Temp = 133 and Rev = 1
Reminder = Temp % 10 = 133 % 10 = 3
Rev = 1 * 10 + 3 = 10 + 3 => 13
Temp= 133 /10 => 13
Third Iteration:
Reminder = 13%10 = 3
Rev = 13 * 10 + 3 => 130 + 3 = 133
Temp= 13/10 = 1
Fourth Iteration:
Reminder= 1 %10 = 1
Rev= 133 * 10 + 1 => 1330 + 1 = 1331
Temp = 1/10 = 0
Here, Temp= 0. It means the while loop fails
if (Number==Rev ) – condition will check whether the user enters integer is exactly equal to the Reverse number or not. If the condition is True, it is a Palindrome. Otherwise, it’s not.
Here, if(1331 ==1331) – TRUE
C Palindrome number program using For Loop
This C program checks whether a given is a Palindrome Number or Not using the For Loop.
#include <stdio.h> int main() { int Num, Temp, Reminder, Rev = 0; printf("\nPlease Enter any num\n"); scanf("%d", &Number); for(Temp=Num; Temp > 0; Temp= Temp/10 ) { Reminder = Temp %10; Rev= Rev *10+ Reminder; } printf("Rev of entered is = %d\n", Rev); if ( Num==Rev ) printf("\n%d is Palindrome.\n", Num); else printf("%d is not.\n", Num); return 0; }
We replaced the While loop of the first example with the For Loop.
C Program to Find the Palindrome Number using Functions
This program checks palindrome numbers using the Functions concept.
#include <stdio.h> int Rev_Integer (int) int main() { int Num, Rev = 0; printf("\nPlease Enter any \n"); scanf("%d", & Num); Rev = Rev_Integer (Num) printf("Inverse of entered = %d\n", Rev); if ( Num == Rev ) printf("\n%d is Palindrome.\n", Num); else printf("%d is not.\n", Num); return 0; } int Rev_Integer (int Num) { int Rem, Rev = 0; for(; Num > 0; Num = Num /10 ) { Rem = Num %10; Rev = Rev * 10 + Rem; } return Rev; }
When the compiler reaches the Rev_Integer (Num) line in the main(), then the compiler jumps to the below function:
int Rev_Integer (int Num)
The last line of this palindrome number program ends with a return Reverse statement.
C Palindrome Number program using Recursion
This program checks palindrome using the Recursive Functions concept in Programming.
#include <stdio.h> int rv_Integer (int) int main() { int Num, rv = 0; printf("\nPlease Enter any value\n"); scanf("%d", & Num); rv = rv_Integer (Num) printf("Reverse of entered is = %d\n", rv); if ( Num == rv ) printf("\n%d is Palindrome.\n", Num); else printf("%d is not.\n", Num); return 0; } int rv_Integer (int Num) { static Reminder, rv = 0; if (Num > 0) { Reminder = Num %10; rv = rv*10+ Reminder; rv_Integer (Num /10); return rv; } else return 0; }
Within this program, when the compiler reaches the rv_Integer (Num) line in the main(), then the compiler will immediately jump to that recursive function:
In this function, the rv_Integer (Num/10) statement will help to call the function Recursively with the updated value. If you miss this statement in this program, after completing the first line, it will terminate. For example, Num = 1331, then the output will be 1
Let’s see the If condition in this C programs.
if (Num > 0) – checks whether the value is greater than 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, the example will end up in infinite executions (Same as Infinite Loop).
C Program for Palindrome Numbers between 1 and 1000
It allows you to enter minimum and maximum values. Next, the C program finds the palindrome Numbers between the Minimum and Maximum values.
#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("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; }
This For Loop restricts the compiler to repeating between Minimum and maximum Variables. I mean, iteration starts at the Minimum and ends at the Maximum variable.
for (Number=Minimum; Number<=Maximum; Number++)
The if condition will check the iteration number is accurately equal to the Reverse number. If it’s true, it is a palindrome number; else, the given value is not.