Friday 19 August 2016

Chapter 5 Exercise 31, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

*5.31 (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+100005.75/1200=10047.9210000+100005.75/1200=10047.92 After two months, the CD is worth 10047.91+10047.915.75/1200=10096.0610047.91+10047.915.75/1200=10096.06 After three months, the CD is worth 10096.06+10096.065.75/1200=10144.4410096.06+10096.065.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 in the sample run.

Enter the initial deposit amount: 10000
Enter annual percentage yield: 5.75
Enter maturity period (number of months): 18
Month CD Value
1 10047.92
2 10096.06
...
17 10846.57
18 10898.54



import java.util.Scanner;
 
public class ProgrammingEx5_31 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  // Enter saving amount
  System.out.print("Enter the initial deposit amount:");
  double saving = input.nextDouble();
 
  System.out.print("Enter annual percentage yield:");
  double interest = input.nextDouble();
  interest /= 12 * 100; // convert interest rate from annual to monthly
 
  System.out.print("Enter maturity period (number of months):");
  double noOfMonths = input.nextDouble();
 
  // print header
  System.out.printf("%7s%12s\n", "Month", "CD value");
 
  for (int i = 1; i <= noOfMonths; i++) {
 
   saving = saving * (1 + interest);
   System.out.printf("%4d%12.2f\n", i, saving);
 
  }
 
  }
 
}

No comments :

Post a Comment