-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOddEvenSum.java
More file actions
28 lines (20 loc) · 809 Bytes
/
OddEvenSum.java
File metadata and controls
28 lines (20 loc) · 809 Bytes
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
public class OddEvenSum {
public static void main(String[] args) {
final int LOWERBOUND = 0;
final int UPPERBOUND = 1000;
int sumOdd = 0;
int sumEven = 0;
int number = LOWERBOUND;
while (number <= UPPERBOUND) {
if (number % 2 == 0) {
sumEven += number; // Same as sumEven = sumEven + number
} else {
sumOdd += number;
}
++number;
}
System.out.println("The sum of odd numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is " + sumOdd);
System.out.println("The sum of even numbers from " + LOWERBOUND + " to " + UPPERBOUND + " is " + sumEven);
System.out.println("The difference between the two sums is " + (sumOdd - sumEven));
}
}