-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumAverageRunningInt.java
More file actions
22 lines (21 loc) · 959 Bytes
/
SumAverageRunningInt.java
File metadata and controls
22 lines (21 loc) · 959 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* Compute the sum and average of running integers from a lowerbound to an upperbound using loop.
*/
public class SumAverageRunningInt { // Save as "SumAverageRunningInt.java"
public static void main (String[] args) {
// Define variables
int sum = 0; // The accumulated sum, init to 0
double average; // average in double
int lowerbound = 111;
int upperbound = 8899;
// Use a for-loop to sum from lowerbound to upperbound
for (int number = lowerbound; number <= upperbound; ++number) {
// The loop index variable number = 1, 2, 3, ..., 99, 100
sum += number; // same as "sum = sum + number"
}
// Compute average in double. Beware that int / int produces int!
average = (double) sum / (double) (upperbound - lowerbound + 1);
// Print sum and average
System.out.println("Sum: " + sum + " Average: " + average);
}
}