Time spent here:

Tuesday, 15 March 2016

SUM OF DIGIT AS PALINDROME(GEEKS FOR GEEKS )

Write a program to check if the sum of digits of a given number is pallindrome number or not.
Input:
The first line of the input contains T denoting the number of testcases.Then each of the T lines contains single positive integer N denoting the value of number.

Output:
Output "YES" if pallindrome else "NO". (without the quotes)

Constraints:
1<=T<=100
1<=N<=1000

Example:
Input:
2
56
98
Output:
YES
NO
SOURCE CODE:
#include <stdio.h>
int k;
int palindrome(int n)
{
    int a[100],count=0,i,j;
    i=0;
    while(n)
    {
        a[i]=n%10;
        n=n/10;
        count++;i++;
  }
if(count==1 || k==0)
    {
        return 1;
    }
j=count-1;
for(i=0;i<count;i++)
{
    if(a[i]!=a[j])
      return 0;
   j--;
 }
return 1;    
}
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
   scanf("%d",&n);
   int sum=0,count=0;k=n;
   while(n)
   {
       sum=sum+(n%10);
       n=n/10;
   }
   if(palindrome(sum))
   {
       printf("YES\n"); 
   }
   else
   {
       printf("NO\n");
   }
}
return 0;
}

No comments:

Post a Comment