-->

program to test whether a number is palindrome or no

//program to test whether a number is palindrome or not
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
   int n,n1,n2,rem,sum=0,count=0;
   printf("enter a number for 'n'\n");
  scanf("%d",&n);
n1=n;
n2=n;
  while(n!=0)
    {
       count=count+1;
       n=n/10;    

   }
count--;
while(n1!=0)
    {
       rem=n1%10;
       sum=sum+rem*(pow(10,count));
       n1=n1/10;
       count--;     
   }
if(sum==n2)
{
printf("it's palindrome\n");
}
else
{
printf("it's not palindrome");
}
getch();
}
--------------------------------------------------------------------------------------------------------------------------------------
logics in mind:
a palindrome number is that which is same from both the sides i.e. from left to right and from right . e.g. 101 or 121 or 1221 etc.
->first we enter a number.
->before we go for further process,we first need to get total digits present in that number. For this, we have used loop (first,look above in code) with a variable 'count'. We have used logic 
                                                                                              ->count=count+1
                                                                                             ->n=n/10
->We decrease the value of 'count' by one. Let's take an example, 121.
                                                   ->we have to reverse it. for this, we first get last digit '1'. To get '1', we divide it                                                                     by 10 and get remainder. 
                                                  ->121 can be written as 100+20+1(in reverse order)
                                                                                        1x102+2x101+1x100
                                                  ->If we look at this expression then we can see the power is decreasing from 2                                                             to 0. So we have to start power from 2 and not from 3. So we have written                                                        count --
->We use again loop to get sum after reversing the entered number. We have also used power(count) in   decreasing order.
->as we get sum then we check that with original value.


No comments:

Post a Comment