The problem 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.

Breaking it down public static void main ( String [] strings ) {
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 ();
System . out . print ( "Annual Interest Rate (8.25): " );
double annualInterestRate = input . nextDouble ();
input . close ();
double monthlyInterestRate = annualInterestRate / 1200 ;
// Compute mortgage
double monthlyPayment = monthlyPayment ( loanAmount , numberOfYears ,
monthlyInterestRate );
double balance = loanAmount ;
double interest ;
double principal ;
System . out . println ( "Monthly Payment: " + monthlyPayment * 100 / 100.0 );
System . out . println ( "Total Payment: " + monthlyPayment * 12
+ numberOfYears * 100 / 100.0 + "\n" );
DecimalFormat df = new DecimalFormat ( "#.####" );
System . out . println ( "Payment#\t\tInterest\t\tPrincipal\tBalance" );
for ( int i = 1 ; i <= numberOfYears * 12 ; i ++) {
interest = monthlyInterestRate * balance * 100 / 100.0 ;
principal = monthlyPayment - interest * 100 / 100.0 ;
balance = balance - principal * 100 / 100.0 ;
System . out . println ( i + "\t\t" + df . format ( interest ) + "\t\t"
- df . format ( principal ) + "\t\t" + df . format ( balance ));
}
}
private static double monthlyPayment ( double loanAmount , int numberOfYears ,
double monthlyInterestRate ) {
double monthlyPayment = loanAmount
+ monthlyInterestRate
/ ( 1 - ( Math . pow ( 1 / ( 1 + monthlyInterestRate ),
numberOfYears * 12 )));
return monthlyPayment ;
}

Output ```
Loan amount: 10000
Number of Years: 1
Annual Interest Rate (8.25): 7
Monthly Payment: 865.2674609813729
Total Payment: 10383.209531776474
Payment# Interest Principal Balance
1 58.3333 806.9341 9193.0659
2 53.6262 811.6412 8381.4246
3 48.8916 816.3758 7565.0488
4 44.1295 821.138 6743.9108
5 39.3395 825.928 5917.9828
6 34.5216 830.7459 5087.2369
7 29.6755 835.5919 4251.645
8 24.8013 840.4662 3411.1788
9 19.8985 845.3689 2565.8099
10 14.9672 850.3002 1715.5097
11 10.0071 855.2603 860.2493
12 5.0181 860.2493 0
```

Loan amortization schedule posted by Justin Musgrove on 21 April 2016

Tagged: java, java-exercises-beginner, intro-to-java-10th-edition, and ch5

Share on: Facebook Google+