In: Computer Science
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.
Steps: 1) Create scanner
2) Prompt the user to enter loan amount and declare double variable and relate to scanner input
3) Prompt the user to enter number of years and declare integer years and relate to scanner input
4) Prompt the user to enter annual interest rate and declare double variable and relate to scanner input
5) Calculate monthly interest rate: first create a double variable called monthlyRate which is equal to annualRate divided by 1200
6) Calculate monthly payement: first create double variable called monthlyPayement which is equal to loanAmount times monthlyRate divied by (1 - 1 / Math.pow(1 + monthlyRate, years * 12));
7) Display in console monthlyPayement
8) Display in console total amount with formula inside of prompt (monthlyPayement*12) * years);
9) Create so called amortization schedule: first create double variable called balance which is equal to loanAmount, principal, interest;
10) System.out.println ("Payment# Interest Principal Balance"); for (int i = 1; i <= years * 12; i++) { interest = monthlyRate * balance; principal = monthlyPayment - interest; balance = balance - principal; System.out.printf("%-13d%-13.2f%-13.2f%.2f\n", i, interest, principal, balance); }
Code For Above Problem:
import java.util.Scanner;
public class AmortizationSchedule {
public static void main(String[] args) {
// Steps: 1) Create scanner
Scanner sc = new Scanner(System.in);
/* 2) Prompt the user to enter loan amount and declare double variable and
* relate to scanner input
*/
System.out.print("Enter Loan Amount: ");
double loanAmount=sc.nextDouble();
/*3) Prompt the user to enter number of years and declare integer years
* and relate to scanner input
*/
System.out.print("Enter Number Of Years: ");
int years=sc.nextInt();
/*4) Prompt the user to enter annual interest rate and declare double
* variable and relate to scanner
*/
System.out.print("Enter Annual Interest Rate: ");
double annualRate=sc.nextDouble();
/*5) Calculate monthly interest rate: first create a double variable
* called monthlyRate which is equal to annualRate divided by 1200
*/
double monthlyRate=annualRate/1200;
/*6) Calculate monthly payement: first create double variable called
* monthlyPayement which is equal to
* loanAmount times monthlyRate divied by (1 - 1 / Math.pow(1 + monthlyRate, years * 12));
*/
double monthlyPayment=(loanAmount*monthlyRate)/(1 - 1 / Math.pow(1 + monthlyRate, years * 12));
/*
* 7) Display in console monthlyPayement
*/
System.out.println("Monthly Payment: "+monthlyPayment);
/*
* 8) Display in console total amount with formula inside of prompt (monthlyPayement*12) * years);
*/
System.out.println("Total Amount: "+(monthlyPayment*12) * years);
/*
* 9) Create so called amortization schedule:
* first create double variable called balance which is equal to loanAmount, principal, interest;
*/
double balance=loanAmount;
double principal,interest;
/*10) System.out.println ("Payment# Interest Principal Balance");
* for (int i = 1; i <= years * 12; i++)
* { interest = monthlyRate * balance;
* principal = monthlyPayment - interest;
* balance = balance - principal;
* System.out.printf("%-13d%-13.2f%-13.2f%.2f\n", i, interest, principal, balance);
* }
*/
System.out.println ("Payment# Interest Principal Balance");
for (int i = 1; i <= years * 12; i++)
{
interest = monthlyRate * balance;
principal = monthlyPayment - interest;
balance = balance - principal;
System.out.printf("%-13d%-13.2f%-13.2f%.2f\n", i, interest, principal, balance);
}
sc.close();
}
}
Sample Run Results:
Enter Loan Amount: 50000
Enter Number Of Years: 2
Enter Annual Interest Rate: 10
Monthly Payment: 2307.2463168758336
Total Amount: 55373.91160502001
Payment# Interest Principal Balance
1 416.67 1890.58 48109.42
2 400.91 1906.33 46203.09
3 385.03 1922.22 44280.87
4 369.01 1938.24 42342.63
5 352.86 1954.39 40388.24
6 336.57 1970.68 38417.56
7 320.15 1987.10 36430.46
8 303.59 2003.66 34426.80
9 286.89 2020.36 32406.44
10 270.05 2037.19 30369.25
11 253.08 2054.17 28315.08
12 235.96 2071.29 26243.79
13 218.70 2088.55 24155.24
14 201.29 2105.95 22049.29
15 183.74 2123.50 19925.79
16 166.05 2141.20 17784.59
17 148.20 2159.04 15625.55
18 130.21 2177.03 13448.52
19 112.07 2195.18 11253.34
20 93.78 2213.47 9039.87
21 75.33 2231.91 6807.96
22 56.73 2250.51 4557.45
23 37.98 2269.27 2288.18
24 19.07 2288.18 -0.00
Images Of Code:
Image Of Sample Run Results: