-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReverseNumber.java
34 lines (32 loc) · 1011 Bytes
/
ReverseNumber.java
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
package com.company;
public class ReverseNumber {
static public int reverse(int n) {
int result = 0;
int rem;
while (n > 0) {
rem = n % 10;
n = n / 10;
result = result * 10 + rem;
}
return result;
}
static public int reverseS(int n) {
String input = String.valueOf(n);
String result = "";
for (int i = input.length()-1; i >= 0; i--) {
result = result + input.charAt(i);
}
int reversedInt = Integer.parseInt(result);
return reversedInt;
}
static public int reverseSBuilder(int n) {
String inputString = String.valueOf(n); // make it string
String stringBuffer = new StringBuffer(inputString).reverse().toString(); //reverse the string
return Integer.parseInt(String.valueOf(stringBuffer));
}
public static void main(String[] args) {
int x = 12;
int y = reverseS(12);
System.out.println(y);
}
}