rockybalboa123 - 1 year ago 77

Java Question

This is the question :

Jalaj was deeply studying in the afternoon and came across a concept

called as reverse of a number Hearing this for the first time, he

was eager to learn and searched the Internet and decided to design a

program, which can reverse any number. But, he could not convert his

logic into code. So, he is now requesting you to design a very

efficient method to reverse a number and print it as a EVEN or ODD, as

the case may be. Input The first line contains T, the number of

test cases. Followed by T lines each contains a single number.

Output: For every test case print the EVEN if reverse of the given

number is even, else print ODD. Constraints: 1<=T<=10000 1<=N<=10

raised to the power 100 Sample Input 5 1 3 6 15 23 Sample Output

ODD 1 ODD 3 EVEN 6 ODD 15 EVEN 23

now this is my code :

`class RevJalNo{`

public static void main(String args[]){

int t,x,y=0,z;

Scanner s=new Scanner(System.in);

t=s.nextInt();

if(t>=1&&t<=10000){

for(int i=0;i<t;i++){

x=s.nextInt();

if(x>=1&&x<=Math.pow(10,100)){

z=x;

while(x!=0){

y=y*10;

y=y+x%10;

x=x/10;

}

if(y%2==0)

System.out.println("EVEN "+z);

else

System.out.println("ODD "+z);

}

}

}

}

}

It is giving the right output but is passing only

Can anybody find out what is the problem ??

Answer Source

10¹⁰⁰ as an upper bound for N won't fit in a 32 bits Java int. So to start

```
x = s.nextInt();
```

will raise an error.

I guess that doing it by reversing the String representation is not an option, so you'll have to use BigInteger in something like

```
class RevJalNo {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
final int t = s.nextInt();
BigInteger w = new BigInteger("2");
for (int i=1; i<t && i<10000; i++) {
BigInteger x = new BigInteger(s.next());
BigInteger y = new BigInteger("0");
while (x.compareTo(BigInteger.ZERO)>0) {
y = y.multiply(BigInteger.TEN);
y = y.add(x.mod(BigInteger.TEN));
x = x.divide(BigInteger.TEN);
}
if (y.mod(w).compareTo(BigInteger.ZERO)==0)
System.out.println("EVEN "+y);
else
System.out.println("ODD "+y);
}
}
}
```