-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlusOnee.java
More file actions
49 lines (35 loc) · 1.01 KB
/
PlusOnee.java
File metadata and controls
49 lines (35 loc) · 1.01 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
import java.util.*;
public class PlusOnee {
// Function: plus one logic
public static int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] < 9) {
digits[i]++;
return digits;
}
digits[i] = 0;
}
int[] result = new int[digits.length + 1];
result[0] = 1;
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Input size
System.out.print("Enter number of digits: ");
int n = sc.nextInt();
int[] digits = new int[n];
// Input digits
System.out.println("Enter digits:");
for (int i = 0; i < n; i++) {
digits[i] = sc.nextInt();
}
// Function call
int[] result = plusOne(digits);
// Output
System.out.println("Result:");
for (int num : result) {
System.out.print(num + " ");
}
}
}