Skip to content

Instantly share code, notes, and snippets.

Created June 15, 2013 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5787834 to your computer and use it in GitHub Desktop.
Save anonymous/5787834 to your computer and use it in GitHub Desktop.
import java.util.*;
/**
* sets up the calculator
*/
public class LoanCalculator {
public static void main(String[] args) {
Scanner console=new Scanner(System.in);
System.out.print("What is the annual percentage rate (APR)? ");
double apr=console.nextDouble();
System.out.print("What is the loan term in years? ");
double loanTerm=console.nextDouble();
System.out.print("What is the loan amount in whole dollars? ");
double amount=console.nextDouble();
double monthlyPayment= calculatePayment( apr, loanTerm, amount);
System.out.printf("the monthly payment is : %.2f\n", monthlyPayment);
producePaymentSchedule(monthlyPayment, apr, loanTerm, amount);
}
/**
* Calculates the monthly payment.
* @return monthly payment
*/
public static double calculatePayment(double apr, double loanTerm, double amount) {
double n = 12 * loanTerm;
double i= apr/1200;
double monthlyPayment=(amount * i * Math.pow(1+i, n))/(Math.pow(1+i, n)-1);
return monthlyPayment;
}
/**
* Create Payment Schedule
* @param monthly payment, apr, loanTerm, and amount
*/
public static void producePaymentSchedule(double monthlyPayment, double apr, double loanTerm, double amount) {
System.out.println(" Payment Schedule ");
System.out.printf(" num Payment Interest Principal Balance \n");
double Balance=amount;
System.out.printf(" $%8.2f\n\n", Balance);
for(int i=1; i<=(loanTerm*12); i++) {
double payment=monthlyPayment;
double interest= (apr/1200)*Balance;
double principal=payment-interest;
Balance=Balance-principal;
System.out.printf("%4d $%6.2f $%6.2f $%6.2f $%8.2f\n", i, payment, interest, principal, Balance);
}
}
}
@pcnoic
Copy link

pcnoic commented Jul 12, 2016

Saw this code on the LinusTechTips forum for the challenge of a useful program under 100 lines of code: you could really nail it if you indented more efficiently

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment