Write a C program to find the sum of individual digits of a positive integer and test given number is palindrome.
(Palindrome number is such number which when reversed is equal to the original number. For example: 121, 12321, 1001 etc.)
#include <stdio.h>
void main()
{
int n, temp, r=0,sum=0, rev = 0;
clrscr();
printf("\n Enter an integer: \n");
scanf("%d", &n);
/* original number is stored at temp */
temp = n;
while (n > 0)
{
r = n % 10;
sum=sum+r; // sum of Individual Numbers
rev = rev * 10 + r;
n = n/10;
}
printf("\n Given number is = %d\n", temp);
printf(ā\n Sum of individual digits of a given number is:%dā, sum);
printf("\n Its reverse is = %d\n", rev);
if (temp == rev)
printf("\n %d Number is a palindrome \n", temp);
else
printf("\n %d is Number is not a palindrome \n", temp);
getch();
}
Comments
Post a Comment