import cs1.Keyboard; /** Calculate the sum, product and average of the first n reciprocals. @author Greg Vogl last modified 2003-09-22 */ public class Firstnrecip { /** Calculate the sum, product and average of the first n reciprocals.
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 double sum = 0; double product = 1; for (int i=1; i<=n; i++) { sum += 1.0/i; product *= 1.0/i; } double average = sum / n; // output results System.out.println(sum + "\tSum"); System.out.println(product + "\tProduct"); System.out.println(average + "\tAverage"); } }