Monday, 21 August 2017

Sum Palindrome

Given a number, reverse it and add it to itself unless it becomes a palindrome or the the count becomes 5 times. If it becomes a palindrome then print that palindrome number, otherwise print -1.

Input: First line of the input contains an integer T denoting the number of test cases. Each test case has a single line containing a number.

Output: Corresponding to each test case, print the palindrome number or -1 as stated above.

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

Example:
Input:
2
23
30
Output:
55
33

                             C-Solution
#include <stdio.h>
int p;
int reverse(int s)
{p=0;
  while(s!=0)
  {  
      p=(p*10)+s%10;
      s=s/10;
  }
  return p;   
}
int main() 

{
   int n,a,c,t,result=0;
   scanf("%d",&n);
   while(n--)
   {
       scanf("%d",&a);t=a;c=5;
      while(c>=0)
      {
            result=reverse(t);//printf("k%dfg\n",result);
            if(result==t)
            {
                printf("%d\n",t);break;
            }
            else
            {   t=result+t;
               // result=result+reverse(result);
             c--;
            }           
      }
      if(c<0)
      printf("-1\n");
   }
   return 0;
}

 

No comments:

Post a Comment