Programming Assignment 4


Due: Wed. Sep. 27 before lab


  1. (25 points) Problem 5.1 of the book. Count positive and negative numbers and compute the average of numbers. Write a program that reads an unspecified number of integers, determines how many positive and negative values have been read, and computes the total and average of the input values (not counting zeros). Your program ends with the input 0. Display the average as a floating-point number.
  2. (25 points) Problem 5.25 of the book. Compute π. You can approximate π using the following series:
    π = 4(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + … + (-1)i+1/(2i - 1))
    Write a program that displays π value for i = 10000, 20000, …, 100000. Use System.out.printf for proper formatting.
  3. (25 points) Problem 5.31 of the book. Financial application: compute CD value. Suppose you put $10,000 into a CD with an annual percentage yield of 5.75%. After one month, the CD is worth
    10000 + 10000 * 5.75 / 1200 = 10047.92
    After two months, the CD is worth
    10047.91 + 10047.91 * 5.75 / 1200 = 10096.06
    After three months, the CD is worth
    10096.06 + 10096.06 * 5.75 / 1200 = 10144.44
    and so on.
    Write a program that prompts the user to enter an amount (e.g., 10000), the annual percentage yield (e.g., 5.75), and the number of months (e.g., 18) and displays a table as shown below. Use System.out.printf to format the money amounts properly.
  4. (25 points) Problem 5.43 of the book. Math: combinations. Write a program that displays all possible combinations for picking two numbers from integers 1 to 7. Also display the total number of all combinations.
  5. Bonus. (10 points) Problem 5.17 of the book. Display pyramid. Write a program that prompts the user to enter an integer from 1 to 15 and displays a pyramid like below.