-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseInterger.java
More file actions
46 lines (36 loc) · 1.21 KB
/
ReverseInterger.java
File metadata and controls
46 lines (36 loc) · 1.21 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
39
40
41
42
43
44
45
46
/* Question 5: Reverse Interger
*Write a program take an interger as input and returns an interger with reversed digit ordering
*/
import java.util.Scanner;
public class ReverseInterger {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = input.nextInt();
input.close();
// calls the reversedInterger method and prints out the result
int reversed = reverseInterger(num);
System.out.println("Reversed number is: " + reversed);
}
// Method to reverse the digit ordering of the interger.
public static int reverseInterger(int num1) {
int reversed = 0;
boolean negative = false;
// check if the number is negative
if (num1 < 0) {
negative = true;
num1 = -num1;
}
// reverse the digit ordering
while (num1 > 0) {
int digit = num1 % 10;
reversed = reversed * 10 + digit;
num1 /= 10;
}
// check if the number is negative and convert it back if true
if (negative) {
reversed = -reversed;
}
return reversed;
}
}