Develop a palindrome program in C using loops

Making a palindrome program in C is very easy. You can develop a code using any loop.

If you are not familiar with loops, I am describing for loop syntax below.

For loop syntax

for(initilization; condition; increment/decrement)
{
statement 1;
statement 2;
}

initilization is the initial value where your counter value starts 
and condition is the last value.

Palindrome Program in C

#include<stdio.h>
#include<conio.h>
void main()
{
int n,r,b=0,temp;
clrscr();
printf("Enter any number to check palindrome");
scanf("%d",&n);

tmp=n;//It is copy of number.
while(tmp>0)
{
r=temp%10;
b=b*10+r;
temp=temp%10;
}

if(b==n)
{
printf("Number is Palindrome");
}
else
{
printf("Number is not palindrome");
}
getch();
}

Program Code Explanation (PCE)

In the above Palindrome Program in C, n is the number entered by a user to check for palindrome number. We assign a new temporary variable temp to store a duplicate copy of n.

We execute a while loop until temp is greater than 0. In this loop, we calculate a reminder(r) value. We have a new variable b which is used to make a reverse number (b=b*10 + r).

Finally, in b we will get a new number if this number is equal to n so the number is palindrome otherwise it is not a palindrome number.

Hope that you understand above program code. Please ask your questions in the comment section below.

Other Programs

Don’t forget to share this program link in your friend’s circle. You can also check our travel blog travelthenature.com

Leave a Comment

error: Don\'t make piracy - Write your own content :)