14 June 2018

sum palindrome

 A given number check it palindrome or not

under following conditions,
1, you can add up to 5times of reversed number is possible.
2, until the addition of 5times it will not palindrome it will returns -1.

explanation:
i.p:
no.of.case
numbers
sample
o.p:
  palindrome or -1 

samples:
i/p:
4
987
979783

23
33
o/p:
66066      (987+789=1776 ,1776+6771=8547 ,8547+7458=16005 ,16005+50061=66066)
-1

55            (23+32=55)
33            


import java.util.*;
public class NewClass17 {
             int reverse(int a)
             {
                 int rev=0;
                 while(a>0)
                 {
                     rev=rev*10+(a%10);
                     a=a/10;
                 }
                 return rev;
             }
             void palindrome(int k)
             {
                 int i;
                 for(i=0;i<5;i++)
                 {
                   int l=reverse(k);
                   if(l==k)
                   {
                       System.out.println(l);
                       break;
                   }else
                   { 
                         k=k+l;
                   }
                 }
                 if(i==5)
                     System.out.println(-1);
             }
            
public static void main(String args[])
             {
                 Scanner s=new Scanner(System.in);
                   int n=s.nextInt();
                   NewClass17 ob=new NewClass17();
                   for(int i=0;i<n;i++)
                   {
                       int k=s.nextInt();
                       ob.palindrome(k);
                   }
             }
}


 

No comments:

Post a Comment