import cs1.Keyboard; /** Calculate the sum, product and average of the first n integers. @author Greg Vogl last modified 2003-09-22 */ public class Firstn { /** Calculate the sum, product and average of the first n integers.
input: integer n
output: sum, product, average @throws IOException to handle bad input */ public static void main (String[] args) { System.out.println("Calculate the sum, product and average of the first n integers."); // get input System.out.println("Type an integer and press Enter."); int n = Keyboard.readInt(); // calculate statistics int sum = 0; double product = 1; for (int i=1; i<=n; i++) { sum += i; product *= i; } float average = (float)sum / n; // output results System.out.println(sum + "\tSum"); System.out.println(product + "\tProduct"); System.out.println(average + "\tAverage"); } }