-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava5.java
More file actions
40 lines (37 loc) · 1.16 KB
/
java5.java
File metadata and controls
40 lines (37 loc) · 1.16 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
import java.util.Scanner;
public class java5 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int input;
while (true) {
System.out.print("Enter a number (negative to quit): ");
input = scanner.nextInt();
if (input < 0) {
break;
}
System.out.println("Sum of odd numbers less than " + input + ": " + ComputeOddSum(input));
System.out.println("Sum of even numbers less than " + input + ": " + ComputeEvenSum(input));
}
scanner.close();
}
public static int ComputeOddSum(int input) {
if (input <= 0) {
return 0;
}
if (input % 2 == 1) {
return input - 1 + ComputeOddSum(input - 1);
} else {
return ComputeOddSum(input - 1);
}
}
public static int ComputeEvenSum(int input) {
if (input <= 0) {
return 0;
}
if (input % 2 == 0) {
return input - 2 + ComputeEvenSum(input - 2);
} else {
return ComputeEvenSum(input - 1);
}
}
}