Ticker

6/recent/ticker-posts

Header Ads Widget

Responsive Advertisement

Palindrome Checker using C

Problem Statement

In this program we will code a basic program to check whether a number is palindrome or not in C programming language.

A palindromic number or numeral palindrome is a number that remains the same when its digits are reversed. Like 16461, for example, it is “symmetrical”. Hence,it is quite clear that if a number is equal to its reversal then it is a palindrome number.




Code

#include

bool palindrome(int n)

{

    int rem=0,rev=0,num=0;

               num=n;

               while(num>0)

               {

               rem=num%10;

               rev=rev*10+rem;

               num=num/10;

               }

               if(rev==n)

               {

                 return true;

               }

               return false;

}

int main()

{

  int n;

  printf("Enter the number ?\n");

  scanf("%d",&n);

  if(palindrome(n))

  {

    printf("PALINDROME"); 

  }

  else

  {

      printf("NOT A PALINDROME");

  }

  return 0;

}

Output

Enter the number ? – 121

PALINDROME 




Explanation 

In this problem we have to check whether the given number is a Palindrome or not.

FINDING THE REVERSE OF A NUMBER


Let us consider an example :
Suppose we have to find the reverse of 535,we require three steps which needs to be performed under while loop condition (while n>0).
1 : Finding Remainder : rem=num%10
So, rem=535%10=5
2 : Finding Reverse : rev=rev*10+rem
So, rev=0+5
3 : Updating number : num=num/10;
So, num=53
But num>0, so we will perform above steps until ‘num’ gets equal to zero.After the last iteration we find rev=535.

PALINDROME CHECKER

Now we can check whether the reverse of a number is equal to itself or not.In this case,535 is a Palindrome Number.

Post a Comment

0 Comments