Repository navigation
Expand file tree
/
Copy pathRemoveZerosInDecimal.java
More file actions
66 lines (56 loc) · 2.18 KB
/
Copy pathRemoveZerosInDecimal.java
File metadata and controls
66 lines (56 loc) · 2.18 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
public class RemoveZerosInDecimal {
/**
* Removes all zeros from the decimal representation of a positive integer.
*
* @param n The positive integer
* @return The integer obtained by removing all zeros
*/
public static long removeZeros(long n) {
// Convert number to string
String numStr = String.valueOf(n);
// StringBuilder to build result without zeros
StringBuilder result = new StringBuilder();
// Iterate through each character and skip zeros
for (char ch : numStr.toCharArray()) {
if (ch != '0') {
result.append(ch);
}
}
// Convert back to long and return
return Long.parseLong(result.toString());
}
// Alternative one-liner solution using replace
public static long removeZerosAlternative(long n) {
return Long.parseLong(String.valueOf(n).replace("0", ""));
}
public static void main(String[] args) {
// Test case 1
long n1 = 1020030;
System.out.println("Input: n = " + n1);
System.out.println("Output: " + removeZeros(n1));
System.out.println("Expected: 123");
System.out.println();
// Test case 2
long n2 = 1;
System.out.println("Input: n = " + n2);
System.out.println("Output: " + removeZeros(n2));
System.out.println("Expected: 1");
System.out.println();
// Additional test cases
long n3 = 1000000;
System.out.println("Input: n = " + n3);
System.out.println("Output: " + removeZeros(n3));
System.out.println("Expected: 1");
System.out.println();
long n4 = 9876543210L;
System.out.println("Input: n = " + n4);
System.out.println("Output: " + removeZeros(n4));
System.out.println("Expected: 987654321");
System.out.println();
// Test alternative method
System.out.println("Testing alternative method:");
System.out.println("Input: n = " + n1);
System.out.println("Output: " + removeZerosAlternative(n1));
System.out.println("Expected: 123");
}
}