Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
import java.util.Scanner;
public class LoanDemo
{
// start main method
public static void main(String[] args)
{
// variables declaration
double loanAmount;
int loanPeriod;
double startingIR = 5.0;
double endingIR = 8.0;
double increment = 1.0 / 8.0;
// create an object for Scanner class
Scanner input = new Scanner(System.in);
// prompt the user to enter the loan amount
System.out.print("Loan Amount: ");
loanAmount = input.nextDouble();
/* prompt the user to enter the loan period in number of years */
System.out.print("Number of Years: ");
loanPeriod = input.nextInt();
// display the resultant table's heading
System.out.printf("\n%-20s%-20s%-20s\n", "Interest Rate", "Monthly Payment", "Total Payment");
/* display the monthly and total payments for each interest rate starting from 5% to 8% with an increment of 1/8 */
for(double interestRate = startingIR; interestRate <= endingIR; interestRate += increment)
{
// compute monthly interest rate
double monthlyIR = interestRate / 1200;
// compute the monthly payment
double monthlyPayment = loanAmount * monthlyIR / (1 - 1 / Math.pow(1 + monthlyIR, loanPeriod * 12));
// compute the total payment
double totalPayment = monthlyPayment * loanPeriod * 12;
// display the results
System.out.printf("%-20.3f%-20.2f%-20.2f\n", interestRate, monthlyPayment, totalPayment);
} // end for
} // end of main method
} // end of LoanDemo class
Advertisements
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.
AcceptLearn more