-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP09_ReverseNumber.java
More file actions
38 lines (32 loc) · 1.06 KB
/
Copy pathP09_ReverseNumber.java
File metadata and controls
38 lines (32 loc) · 1.06 KB
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
30
31
32
33
34
35
36
37
38
package programs;
/**
* ============================================================
* PROGRAM 09: Reverse an Integer Number
* ============================================================
* Problem: WAP to reverse the digits of an integer number.
* - Input : 12345
* - Output: 54321
* - Input : -987
* - Output: -789
* ============================================================
*/
public class P09_ReverseNumber {
public static int reverseInteger(int n) {
boolean isNegative = n < 0;
n = Math.abs(n);
int reversed = 0;
while (n > 0) {
int lastDigit = n % 10;
reversed = (reversed * 10) + lastDigit;
n /= 10;
}
return isNegative ? -reversed : reversed;
}
public static void main(String[] args) {
int[] numbers = {12345, 987654, 100, 7, -456, 12003};
System.out.println("=== INTEGER REVERSAL ===");
for (int num : numbers) {
System.out.printf(" Original: %7d -> Reversed: %7d%n", num, reverseInteger(num));
}
}
}