Written by Sai Sandeep Thota on January 10, 2013 in C > Programming

C Program to Find Given Number is Palindrome or Not

GET ALERTS:

Get our Latest updates delivered to your mailbox!

Palidrome is number, phrase or a word which is read same way from either direction. In this program we are declaring four variables a, b, r, x. x will be the input given by the user and we’ll copy the value of x into the variable a.

Explanation:
#1. r=a%10 gets the last digit of the given number (x).
#2. b=(b*10)+r multiplies the b value with 10 and adds r.
#3. a=a/10 divides a value with 10.

Example:
x = 141.
#1. r = a%10 => 141%10 => 1.
b = (b*10)+4 => (0*10)+1 => 1.
a = a/10 => 14.

#2. r = a%10 => 14%10 => 4.
b = (b*10)+4 => (1*10)+4 => 14.
a = a/10 => 1

#3. r = a%10 => 1%10 => 1.
b = (b*10)+1 => (14*10)+1 => 141
a = a%10 = 0.

So as we are checking r==b here in this case r=141 and b=141. Hence the number is a palindrome.

#include<stdio.h>
#include<conio.h>

void main()
{
	int x, b=0, r, a;
	clrscr();
	printf("Enter x Value:");
	scanf("%d",&x);
	a=x;
	while(a>0)
	{
		r=a%10;
		b=(b*10)+r;
		a=a/10;
	}
	if(x==b)
	{
		printf("The Number %d is a Palidrome.",x);
	}
	else
	{
		printf("The Number %d is not a Palindrome!!",x);
	}
	getch();
}

Output:
palindrome-c

{ 1 comment… read it below or add one }

Himadri Desai April 18, 2013 at 7:46 pm

Nice explanation …thx

Reply

Leave a Comment

Previous post:

Next post: