# Palindrome Program in C

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.

2. Reverse a given number and compare it with the original given value.
3. If they matched correctly, then it is a Palindrome number. Otherwise, it is not.

## Palindrome Number Program in C using 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;

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;
}```
``````Please Enter any value
1331

Rev of entered = 1331
1331 is Palindrome.``````

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;

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;
}```
``````Please Enter any num
191
Rev of entered is = 191

191 is Palindrome.``````

We replaced the While loop of the first example with For Loop.

## C Palindrome Number program using Functions

This C program checks palindrome numbers using the Functions concept.

```#include <stdio.h>

int Rev_Integer (int)

int main()
{
int Num, Rev = 0;

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;
}```
``````Please Enter any
4554
Inverse of entered = 4554

4554 is Palindrome.``````

When the compiler reaches to 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 C program check palindrome using the Recursive Functions concept in Programming.

```#include <stdio.h>

int rv_Integer (int)

int main()
{
int Num, rv = 0;

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;
}```
``````Please Enter any value
9889
Reverse of entered is = 9889

9889 is Palindrome.``````

Within this program, when the compiler reaches to rv_Integer (Num) line in the main() then the compiler will immediately jump to that recursive function:

In this function, 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 & 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.