The problem
Write a program that lets the user enter the loan amount and loan period in number of years and displays the monthly and total payments for each interest rate starting from 5% to 8%, with an increment of 1/8.
Breaking it down
public static void main(String[] args) {
double annualInterestRate = 5.00;
Scanner input = new Scanner(System.in);
System.out.printf("Loan amount: ");
double loanAmount = input.nextDouble();
System.out.print("Number of Years: ");
int numberOfYears = input.nextInt();
input.close();
System.out.printf("%-1s%20s%20s\n", "Interest Rate", "Monthly Payment",
"Total Payment");
for (; annualInterestRate <= 8.00; annualInterestRate += 0.125) {
double monthlyInterestRate = annualInterestRate / 1200;
double monthlyPayment = loanAmount
+ monthlyInterestRate
/ (1 - 1 / Math.pow(1 + monthlyInterestRate,
numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
// Displaying formatted info
System.out.printf("%-1.3f%s%17.2f%24.2f \n", annualInterestRate,
"%", ((int) (monthlyPayment * 100) / 100.0),
((int) (totalPayment * 100) / 100.0));
}
}
Output
Loan amount: 100000
Number of Years: 10
Interest Rate Monthly Payment Total Payment
5.000% 1060.65 127278.61
5.125% 1066.77 128013.06
5.250% 1072.91 128750.04
5.375% 1079.07 129489.53
5.500% 1085.26 130231.53
5.625% 1091.46 130976.04
5.750% 1097.69 131723.06
5.875% 1103.93 132472.58
6.000% 1110.20 133224.60
6.125% 1116.49 133979.11
6.250% 1122.80 134736.11
6.375% 1129.13 135495.60
6.500% 1135.47 136257.57
6.625% 1141.85 137022.01
6.750% 1148.24 137788.93
6.875% 1154.65 138558.32
7.000% 1161.08 139330.17
7.125% 1167.53 140104.48
7.250% 1174.01 140881.24
7.375% 1180.50 141660.46
7.500% 1187.01 142442.12
7.625% 1193.55 143226.22
7.750% 1200.10 144012.75
7.875% 1206.68 144801.72
8.000% 1213.27 145593.11