Palindrome Program in C

Write a Program for Palindrome in C Programming to Check Whether the Number is Palindrome or Not. For this Palindrome in C program, we use while Loop, For Loop, Functions, and Recursion. We also show you how to write a C Program for Palindrome Numbers from 1 to 1000.

Palindrome in C: Any number could be Palindrome in C if it remained the same when we reversed it. For example, 191 is a palindrome number because this number remains the same after reversing the number.

The following steps are the standard procedure to check for the Palindrome Number in C Programming.

  1. Please enter any number to check for Palindrome in C.
  2. Reverse a given number
  3. Compare the original given value with the reverse value.
  4. If they matched correctly, then it is a Palindrome number. Otherwise, it is not a Palindrome number in C programming.

Palindrome in C using While Loop

This C program for Palindrome allows the user to enter any positive integer and. Next, this C program checks whether the given number is Palindrome Number or Not using the While Loop

/* Palindrome Program in C using While Loop */
#include <stdio.h>

int main()
{
   int Number, Temp, Reminder, Reverse = 0;
 
   printf("\nPlease Enter any number to Check for Palindrome\n");
   scanf("%d", &Number);
  
   //Helps to prevent altering the original value
   Temp = Number;

   while ( Temp > 0)
   {
      Reminder = Temp %10;
      Reverse = Reverse *10+ Reminder;
       Temp = Temp /10;
   }
 
   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;
}
Please Enter any number to Check for Palindrome
1331

Reverse of entered number is = 1331
1331 is Palindrome Number.

In this C palindrome program, We assign the original value 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 number is greater than 0

User Entered value in this C palindrome number program : Number = 1331 and Reverse = 0

Temp = Number

It means, Temp = 1331

First Iteration

Reminder = Temp %10
Reminder = 1331%10 = 1

Reverse = Reverse *10 + Reminder
Reverse = 0 * 10 + 1 = 0 + 1 => 1

Temp = Temp /10 => 1331 /10
Temp = 133

Second Iteration

From the first Iteration of palindrome program in c, the values of both Temp and Reverse has changed as Temp = 133 and Reverse = 1

Reminder = Temp % 10 = 133 % 10 = 3

Reverse = 1 * 10 + 3 = 10 + 3 => 13

Temp = Temp /10 = 133 /10 => 13

Third Iteration: Temp = 13 and Reverse = 13

Reminder = Temp %10 = 13%10 = 3

Reverse = 13 * 10 + 3
Reverse = 130 + 3 => 133

Temp = 13/10 = 1

Fourth Iteration: Temp = 1 and Reverse = 133

Reminder = 1 %10 = 1

Reverse = 133 * 10 + 1
Reverse = 1330 + 1 = 1331

Temp = 1/10
Temp = 0

Here, Number = 0. It means, the while loop fails

if ( Number == Reverse ) – condition will check whether the user enter number is exactly equal to the Reverse number or not. If condition is True, it is a Palindrome Number. Otherwise, it’s not a Palindrome number.

if ( Number == Reverse ) means if(1331 ==1331) – TRUE, Palindrome

C Palindrome using For Loop

This program for palindrome in c check whether a given number is Palindrome Number or Not using the For Loop

/* Program for Palindrome in C using For Loop */
#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;
}
Please Enter any number to Check for Palindrome
191
Reverse of entered number is = 191

191 is Palindrome Number.

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

Palindrome Number using Functions

This program for Palindrome in c check a number is Palindrome Number or Not using Functions concept.

/* Program for Palindrome in C using Functions */
#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;
}
Please Enter any number to Check for Palindrome
4554
Reverse of entered number is = 4554

4554 is Palindrome Number.

When the C compiler reaches to Reverse_Integer (Number) line in the main() program then the C compiler jumps to below function:

int Reverse_Integer (int Number)

The last line of this palindrome program ends with a return Reverse statement.

Palindrome program using Recursion

This C program checks the number is Palindrome Number using the Recursive Functions concept in C Programming.

/* Program for Palindrome in C using Recursion */
#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;
}
Please Enter any number to Check for Palindrome
9889
Reverse of entered number is = 9889

9889 is Palindrome Number.

Within this Palindrome in c program, when the compiler reaches to Reverse_Integer (Number) line in the main() program then the compiler will immediately jump to below function:

int Reverse_Integer (int Number)

In this function, Reverse_Integer (Number /10) statement will help to call the function Recursively with the updated value. If you miss this statement in the palindrome program, after completing the first line, it will terminate. For example, Number = 1331 then the output will be 1

Let’s see the If condition in this C program

if (Number > 0) – checks the number is greater than 0 or not. For Recursive functions, it is essential to place a condition before using the function recursively. Otherwise, the program will end up in infinite executions (Same like Infinite Loop).

C Program for Palindrome Numbers between 1 and 1000

This palindrome program allows you to enter a minimum and maximum values. This program finds the Palindrome Number 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("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;
}
Palindrome Numbers in C 2

This For Loop restrict the compiler to repeat between Minimum & Maximum Variables. I mean, iteration starts at Minimum and ends at the Maximum variable.

for (Number=Minimum; Number<=Maximum; Number++)

if(Number==Reverse) -– condition will checks the iteration number is accurately equal to the Reverse number. If it’s true, it is a Palindrome else the given number is not Palindrome number.