Tuesday, July 15, 2008

Check whether it is a palindrome

/* Write a C program to reverse a given integer number and check *
* whether it is a palindrome. Output the given numbers with suitable*
* message */

#include stdio.h
#include conio.h

void main()
{
int num, temp, digit, rev = 0;

clrscr();

printf("Enter an integer\n");
scanf("%d", &num);

temp = num; /* original number is stored at temp */

while(num > 0)
{
digit = num % 10;
rev = rev * 10 + digit;
num /= 10;
}

printf("Given number is = %d\n", temp);
printf("Its reverse is = %d\n", rev);

if(temp == rev )
printf("Number is a palindrome\n");
else
printf("Number is not a palindrome\n");
}
/*------------------------
Output
RUN 1
Enter an integer
12321
Given number is = 12321
Its reverse is = 12321
Number is a palindrome

RUN 2
Enter an integer
3456
Given number is = 3456
Its reverse is = 6543
Number is not a palindrome
-----------------------------------*/

The main point to note is the logic in the program. 121 can be written as 100+20+1.

that is 1*100+2*10+1. This principal is used. So first the number is saved on other variable. Then divided by 10. the remainders are added in the same above way to get the number. Now this number is compared to duplicate copy of original number to check its same or not. If its same we will print it as a palindrome or else the otherwise

No comments: