HW4.22

HW4.22

HW4.22

 

 1 import java.util.Scanner;
 2 
 3 public class Solution
 4 {
 5     public static void main(String[] args)
 6     {
 7         Scanner input = new Scanner(System.in);
 8 
 9         System.out.print("Loan Amount: ");
10         double loanAmount = input.nextDouble();
11         System.out.print("Number of Years: ");
12         int numberOfYears = input.nextInt();
13         System.out.print("Annual Interest Rate: ");
14         double interestRate = input.nextDouble();
15 
16         input.close();
17 
18         double monthlyPayment = loanAmount * (interestRate / 12) / 
19             (1 - 1 / Math.pow(1 + interestRate / 12, numberOfYears * 12));
20         System.out.println("Monthly Payment: " + monthlyPayment);
21         double totalPayment = monthlyPayment * numberOfYears * 12;
22         System.out.println("Total Payment: " + totalPayment);
23 
24         System.out.printf("%s\t\t%s\t\t%s\t\t%s", "Payment#", "Interest", "Principal", "Balance");
25         System.out.println();
26 
27         double interest, principal;
28         double balance = loanAmount;
29 
30         for(int i = 1; i <= numberOfYears * 12; i++)
31         {
32             interest = interestRate / 12 * balance;
33             principal = monthlyPayment - interest;
34             balance -= principal;
35             System.out.printf("%d\t\t%f\t\t%f\t\t%f", i, interest, principal, balance);
36             System.out.println();
37         }
38     }
39 }