-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoanCompare.java
More file actions
55 lines (46 loc) · 2.53 KB
/
LoanCompare.java
File metadata and controls
55 lines (46 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
Rahim Siddiq
Apr 21 2023
Financial application: Compare loans with various interest rates
*/
import java.util.Scanner;
public class LoanCompare
{
public static void main(String[] args)
{
// Program variables
int numberOfYears;
double loanAmount, annualInterestRate, monthlyInterestRate, monthlyPayment, totalPayment;
// Title of program and description of program function to user
System.out.println(" ====================================================================");
System.out.println(" ============== Financial Application: Compare Loans ================");
System.out.println(" ====================================================================");
System.out.println(" This program calculates the monthly payments for a loan with ");
System.out.println(" different interest rates and displays a table of payments ");
System.out.println(" --------------------------------------------------------------------");
System.out.println();
// Scanner object for input
Scanner input = new Scanner(System.in);
// User input
System.out.print(" Enter the loan amount: ");
loanAmount = input.nextDouble();
System.out.print(" Enter the number of years: ");
numberOfYears = input.nextInt();
// Display the header for the table
System.out.printf("\n %-20s %-20s %-20s \n", "Interest Rate", "Monthly Payment", "Total Payment");
System.out.println(" --------------------------------------------------------------------");
// Loop through interest rates from 5% to 8%, with an increment of 1/8
for (annualInterestRate = 5.0; annualInterestRate <= 8.0; annualInterestRate += 0.125)
{
// Calculate monthly interest rate and monthly payment
monthlyInterestRate = annualInterestRate / 1200; // divide by 12 multiply by 100 = 1200
// Calculate present value using annuity formula
monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
totalPayment = monthlyPayment * numberOfYears * 12;
// Display the monthly payment and total payment for the current interest rate .3f for rate .2f for dollar amounts
System.out.printf(" %-5.3f%% %-20.2f %-20.2f \n", annualInterestRate, monthlyPayment, totalPayment);
// Close the Scanner to prevent resource leak
input.close();
}
}
}