Friday, 19 August 2016

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

**5.22 (Financial application: loan amortization schedule) The monthly payment for a given loan pays the principal and the interest. The monthly interest is computed by multiplying the monthly interest rate and the balance (the remaining principal). The principal paid for the month is therefore the monthly payment minus the monthly interest. Write a program that lets the user enter the loan amount, number of years, and interest rate and displays the amortization schedule for the loan. Here is a sample run: Note The balance after the last payment may not be zero. If so, the last payment should be the normal monthly payment plus the final balance.
   
    Loan Amount: 10000
    Number of Years: 1
    Annual Interest Rate: 7
    Monthly Payment: 865.26
    Total Payment: 10383.21
    Payment# Interest Principal Balance
    1               58.33    806.93     9193.07
    2               53.62    811.64     8381.43
    ...
    11             10.0      855.26     860.27
    12             5.01      860.25     0.01

Hint: Write a loop to display the table. Since the monthly payment is the same for each month, it should be computed before the loop. The balance is initially the loan amount. For each iteration in the loop, compute the interest and principal, and update the balance. The loop may look like this:

for (i = 1; i <= numberOfYears * 12; i++) {
interest = monthlyInterestRate * balance;
principal = monthlyPayment - interest;
balance = balance - principal;
System.out.println(i + "\t\t" + interest
+ "\t\t" + principal + "\t\t" + balance);
}


import java.util.Scanner;
 
public class ProgrammingEx5_22 {
 
 public static void main(String[] args) {
  // Create a Scanner
  Scanner input = new Scanner(System.in);
 
  // Enter loan amount
  System.out.print("Loan amount:");
  double loanAmount = input.nextDouble();
 
  // Enter number of years
  System.out.print("Enter number of years:");
  int numberOfYears = input.nextInt();
 
  // Enter yearly interest rate
  System.out.print("Annual interest rate:");
  double annualInterestRate = input.nextDouble();
 
  double monthlyInterestRate = annualInterestRate / 1200;
  // Calculate payment
  double monthlyPayment = loanAmount
    * monthlyInterestRate
    / (1 - 1 / Math
      .pow(1 + monthlyInterestRate, numberOfYears * 12));
  double totalPayment = monthlyPayment * numberOfYears * 12;
 
  // Display results
  System.out.println("Monthly payment:" + (int) (monthlyPayment * 100)
    / 100.0);
  System.out.println("Total payment:" + (int) (totalPayment * 100)
    / 100.0);
 
  // Printing table header
 
  System.out.printf("%-18s%-18s%-18s%-18s\n", "Payment#", "Interest",
    "Principal", "Balance");
 
  double interest, balance = loanAmount, principal;
 
  for (int i = 1; i <= numberOfYears * 12; i++) {
   interest = monthlyInterestRate * balance;
   principal = monthlyPayment - interest;
   balance = balance - principal;
   System.out.printf("%-18d%-18.2f%-18.2f%-18.2f\n", i, interest,
     principal, balance);
  }
 
 }
 
}

No comments :

Post a Comment