C Programming Reference

C Programming Reference >> C Programming Short Notes >>

22/07/07 - 1418 Views - Ratings :   4.5 of 5 / 14 Votes


Palindrome Determination Using A C Program

Level - Begineer
      

This a comparitevely very easy article in which we develop a program to determine wether a number is palindrome or not.

In mathematics palindrome is a number which when reverse is a same number. For example 2332 is a palindrome but 2323 is not. You must have clear understanding of this concept.

Now what we will be doing is we will be seprating each digits in a number and then reverse them and see if the are exactly same as original. Well we can seprate digits as a string but thier is a more effective and mathematical method for determination which does not depend upon the number of digits in number.

In this logic we will seprate each digit one by one using % and / divison arithemetic operators. It should be noted that X % 10 returns the last digit in X while in a integer X/10 truncates X last digit.

Also while reversing the digits we will be summing the reversed number to according to thier weight (decimal postion). In this way we will be able to reach correct value of reverse number.

Now when this sum is compared to original if they are equal number is a palindrome otherwise not.

Only limitation in this program is the range of the variable int or other you used. The program will work only upto that number and not a value larger than that.

Source code below is written in C and is implementation of concept above, It produces a list of prime number as output.

#include <stdio.h>
int main()
               
{
  int number,temp,remainder,sum=0;
  printf("\n\nEnter no: ");
  scanf("%d",&number);
  temp = number; //Value of temp stores unmodified n value

  while(number>0)
    {
      remainder=number%10; //Gets Last Digit
      number/=10;          //Truncates Last Digit
      sum=sum*10 + remainder; //Builds value of reversed number
     }

   if (sum==temp)
     printf ("\nThe Number Is A Palindrome ");
   else
     printf ("\nThe Number Is Not A Palindrome ");

   getch ();
   return 0;
   
}
 

 

Reader Comments -

Author Comments
Add Comments 


Name :    
Reply :   


Rating :

Code :
Code

 

 
© 2006 cencyclopedia.com