Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Scanner;
  2. class hw {
  3.  
  4.     public static void main(String[] strings) {
  5.  
  6.  
  7.         Scanner cs = new Scanner(System.in);
  8.  
  9.         System.out.print("\nLoan Amount: ");
  10.         double loanAmount = cs.nextDouble();
  11.        
  12.         System.out.print("Number of Years: ");
  13.         int years = cs.nextInt();
  14.        
  15.         System.out.print("Annual Interest Rate: ");
  16.         double annualRate = cs.nextDouble();
  17.  
  18.         double monthlyRate = annualRate / 1200;
  19.         double monthlyPayment = loanAmount * monthlyRate / (1 - 1 / Math.pow(1 + monthlyRate, years * 12));
  20.  
  21.         System.out.printf("\nMonthly Payment: %.2f\n", monthlyPayment);
  22.         System.out.printf("Total Payment: %.2f\n\n", (monthlyPayment * 12) * years);
  23.  
  24.         double balance = loanAmount, principal, interest;
  25.        
  26.         System.out.println("Payment#\tInterest\tPrincipal\tBalance");
  27.         for (int i = 1; i <= years * 12; i++) {
  28.             interest = monthlyRate * balance;
  29.             principal = monthlyPayment - interest;
  30.             balance = balance - principal;
  31.             System.out.printf("%-13d%-13.2f%-13.2f%.2f\n", i, interest, principal, balance);
  32.         }
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement