-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseNumber.java
More file actions
40 lines (36 loc) · 1.19 KB
/
Copy pathReverseNumber.java
File metadata and controls
40 lines (36 loc) · 1.19 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
package interviewprograms.src;
/*
1. Take input num.
2. Convert number to string.
3. Take another string reverse and initialise it to empty.
4. Extract each character from input string from rightside and add it to rev. var.
5. Repeat this process for all chars in string.
*/
public class ReverseNumber {
public static void main(String[] args) {
Integer number=12345;
System.out.println("Original number(I): "+ number);
String numberString=String.valueOf(number);
String reverse="";
for(int i=numberString.length()-1; i>=0; i--){
reverse= reverse+numberString.charAt(i);
}
System.out.println("Reverse(I): "+ Integer.valueOf(reverse));
System.out.println("Another Way-->");
int num= 987654321, mod, rev=0;
System.out.println("Original number(II): "+ num);
while(num!=0){
mod=num%10;
rev=rev*10+mod;
num=num/10;
}
System.out.println("Reverse(II): "+ rev);
}
}
/*
run:
Original number(I): 12345
Reverse(I): 54321
Original number(II): 987654321
Reverse(II): 123456789
*/