Writer
an interactive C program to check whether the given string is a palindrome or
not, using
pointers.
Ans
void main()
{
// get the number to check
long int n;
printf("ENTER A NUMBER: ");
scanf("%ld",&n);
long int n1,mod;
// make a copy of the original number in n1
n1=n;
// store the reversal in rev
long int rev=0;
while(n>0)
{
// grab the rightmost digit of n
mod = n%10;
// shift all digits of rev left, then add in
the new digit
rev = rev * 10 + mod;
// shift all digits of n to the right
(removing the rightmost one)
n = n / 10;
}
// after the loop, rev will have the opposite
digit ordering of n1;
// if these two values are equal, then we
have a palindrome
if (n1 == rev)
printf("%ld is a palindrome\n",n1);
else
printf("%ld is not a
palindrome\n",n1);
}
No comments:
Post a Comment