-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem7.java
More file actions
29 lines (28 loc) · 909 Bytes
/
Problem7.java
File metadata and controls
29 lines (28 loc) · 909 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
public class Problem7 {
// Reverse Integer
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int x=obj.nextInt();
Solution7 sc=new Solution7();
System.out.println(sc.reverse(x));
obj.close();
}
}
class Solution7{
public int reverse(int x){
int revNum=0,rem=0;
while(x!=0){
rem=x%10;
x /=10;
// Check for overflow before multiplying
if (revNum > Integer.MAX_VALUE / 10 || (revNum == Integer.MAX_VALUE / 10 && rem > 7)) {
return 0; // Overflow for positive numbers
}else if (revNum < Integer.MIN_VALUE / 10 || (revNum == Integer.MIN_VALUE / 10 && rem < -8)) {
return 0; // Overflow for negative numbers
}
revNum=(revNum*10)+rem;
}
return revNum;
}
}