C Program to Check Whether a Number is Palindrome or Not

by | Jan 31, 2023 | C, C Programs

Home » C » C Programs » C Program to Check Whether a Number is Palindrome or Not

Introduction

In this C program, we will learn to write a program that will check whether a number is a Palindrome or not.

Palindrome means when the reverse of a number is equal to the original number.

For example, 96569 is a palindrome, but 99665 is not a palindrome.

Go through the C language concepts written below to get a better understanding.

1. Operators in C

2. if-else statement

3. while and do-while loop

Program

#include<stdio.h>
int main()
{
int x;
printf("enter a number:");
scanf("%d",&x);


int org=x; // x is copied to variable org for further use
int rem=0; // rem is used for remainder
int z=0;
while(x>0)
{
rem=x%10;
z=z*10+rem; // formation of reversed integer
x=x/10; // elimination of last digit
}
printf("Reversed number is: %d",z);


if(z==org) // condition check
{
printf("\n%d is a palindrome",org);
}
else
{
printf("\n%d is not a palindrome",org);
}
return 0;
}

 

Output

Explanation

int org=x; here the value of ‘x’ is copied to some other variable named ‘org’ because we want the same value in further code and w cannot use ‘x’ since its value will get change.

The above code is the same as code we write in ‘reverse of a number’ except the last part of if-else statement.

if(z==org)

According to the definition of palindrome if the statement written above gives a true result then it will print that the number entered by the user is a palindrome otherwise it is not.

1010101 is a palindrome because its reverse is also the same as the original value.

 

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author