Palindrome Program in C using the iterative method

In this tutorial you will learn how to write a program in C to check a given number is palindrome or not using iterative method.

Before moving directly on the writing the program to check whether a given number is palindrome or not, you should know

What is Palindrome Number?

A Palindrome number is a number which reverse is equal to the original number means number itself.

For example : 121, 111, 1223221, etc.

In the above example you can see that 121 is a palindrome number. Because reverse of the 121 is same as 121.

How our program will behave?

Suppose if someone gives an input 121 then our program should print “the given number is a palindrome”.

And if someone given input 123 the our program should print “the given number is not a palindrome number”.

C program for palindrome number using iterative method

#include<stdio.h>
#include<conio.h>
void main(){
     int n, reverse = 0, temp;
     printf("enter the number :");
     scanf("%d",&n);
     temp = n;
     while(temp!=0){        
        reverse = reverse*10 + temp%10;        
        temp=temp/10;    
        }
     if(reverse == n){
        printf("The given number is a palindrome");
     }
     else{
        printf("The given number is not a palindrome");
     }
     getch();
 }

Output 1:

enter the number :231
The given number is not a palindrome

Output 2:

enter the number :121
The given number is a palindrome

Explanation of palindrome number c program using Iterative method

  • In the above program there are three variables ‘n’, ‘reverse’ and ‘temp’.
  • Variable ‘n’ is to store the input given by the user, variable ‘reverse’ to hold the number after reverse and ‘temp’ variable is storing original input number to persist the original number. Later we will use temp variable to match with after reverse of the number.
  • While loop is written to reverse the digit of the number given by the user as input.
  • After the reverse of the number, it will store in ‘reverse’ variable.
  • And at last we will compare value of reverse variable with temp variable which is holding original number.
  • If both are equal then it is palindrome number otherwise it is not a palindrome number.

I believe palindrome program in C is now clear to you.

Leave a Comment