To check whether the number is palindrome or not

To check whether the number is palindrome or not

Question:

Write a program to check whether the given number is palindrome or not.

Logic:

  • Get the input number from the user.
  • Then reverse the given number using any algorithm.
  • And compare the given number with reversed number.
  • Print the output as palindrome or not.

Algorithm:

  1. Get the input value from the user.
  2. Find the last digit of the number.
  3. Then reverse the number using some calculation.
  4. Removes the last digit by dividing the number with 10.
  5. Repeat step 2 to 4 until it ends.
  6. Then print the valid output.

Program:

[code lang=”c”]
#include<stdio.h>
#include<math.h>
void main()
{
int a,r,sum=0,b;
clrscr();
printf("enter the number:");
scanf("%d",&a);
b=a;
while(a>0) //reverse the number
{
r=a%10; //finds the last digit
sum=r+sum*10; //reversing number
a=a/10; //removes the last digit
}
if(b==sum) //compare the given number with reversed number
{
printf("Given number is palindrome:");
}
else
{
printf("Given number is not palindrome:");
}
getch();
}
[/code]

You might also like:

To print all palindromes in a word

 

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

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