Friday 19 August 2016

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

*5.30 (Financial application: compound value) Suppose you save $100 each month into a savings account with the annual interest rate 5%. So, the monthly interest rate is 0.05 / 12 = 0.00417. After the first month, the value in the account becomes

 100(1+0.00417)=100.417100(1+0.00417)=100.417

After the second month, the value in the account becomes

(100+100.417)(1+0.00417)=201.252(100+100.417)(1+0.00417)=201.252

After the third month, the value in the account becomes

(100+201.252)(1+0.00417)=302.507(100+201.252)(1+0.00417)=302.507

 and so on. Write a program that prompts the user to enter an amount (e.g., 100), the annual interest rate (e.g., 5), and the number of months (e.g., 6) and displays the amount in the savings account after the given month.

import java.util.Scanner;
 
public class ProgrammingEx5_30 {
 
 public static void main(String[] args) {
  Scanner input = new Scanner(System.in);
  // Enter saving amount
  System.out.print("Enter monthly saving:");
  double saving = input.nextDouble();
 
  System.out.print("Enter annual interest rate:");
  double interest = input.nextDouble();
  interest /= 12 * 100; // convert interest rate from annual to monthly
 
  System.out.print("Enter number of months:");
  double noOfMonths = input.nextDouble();
  double TotalSaving = 0;
 
  for (int i = 1; i <= noOfMonths; i++) {
 
   TotalSaving = (TotalSaving + saving) * (1 + interest);
 
  }
 
  System.out.println("Total saving is " + TotalSaving);
 }
}

No comments :

Post a Comment