-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverseNum.java
More file actions
71 lines (40 loc) · 1.14 KB
/
Copy pathreverseNum.java
File metadata and controls
71 lines (40 loc) · 1.14 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Create a class ReverseCalculator and implement the logic to reverse a number
// i.e., if the input is provided as 27, then the output should be displayed as 72,
// using appropriate control structures.
public class reverseNum {
// method using while loop
public int calcuReverse(int num) {
int reverse = 0;
while(num > 0) {
reverse = reverse*10 + num % 10;
num /= 10;
}
return reverse;
}
// method using for loop
public int forReverse(int num) {
int reverse = 0;
for(; num>0; num /= 10) {
reverse = reverse *10 + num% 10;
}
return reverse;
}
// method using while loop
public int whileReverse(int num) {
int reverse = 0;
if( num == 0) {
return reverse;
}
do {
reverse = reverse*10 + num%10;
num /= 10;
}while(num < 0);
return reverse;
}
public static void main(String [] args) {
reverseNum rNum = new reverseNum();
System.out.println(rNum.calcuReverse(27));
System.out.println(rNum.forReverse(56));
System.out.println(rNum.forReverse(89));
}
}