You are on page 1of 2

ackage PROBLEM_2; import java.util.*; import java.math.

*; public class Solution_2 { public static void main(String[] args) { // Declare variables for inputs double loanAmount = 0; // original loan amount int numMonths = 0; // loan period double annualInterestRate = 0; // percentage: 7 --> 7% // Create a new Scanner object Scanner inReader = new Scanner (System.in); // Prompt & read loan amount System.out.println ("Enter loan amount: "); loanAmount = inReader.nextDouble(); // Verify loanAmount not negative while (loanAmount < 0) { System.out.println ("Loan amount cannot be negative !!! "); System.out.println ("Enter loan amount: "); loanAmount = inReader.nextDouble(); } // Prompt & read loan period in months System.out.println ("Enter loan period in months: "); numMonths = inReader.nextInt(); // Verify loan period not negative while (numMonths < 0) { System.out.println ("Loan period cannot be negative !!! "); System.out.println ("Enter loan period in moths: "); numMonths = inReader.nextInt(); } // Prompt & read loan annual interest rate System.out.println ("Enter annual interest rate: "); annualInterestRate = inReader.nextDouble(); // Verify annual interest rate not negative while (annualInterestRate < 0) { System.out.println ("Annual interest rate cannot be nega tive !!! "); System.out.println ("Enter annual interest rate: "); annualInterestRate = inReader.nextDouble(); } // Compute monthly payment double monthlyInterestRate = annualInterestRate / 1200; double monthlyPayment = (loanAmount * monthlyInterestRate) / (1 - (1/Math.pow((1 + monthlyInterestRate), numMonths)));

// Compute total payment double totalPayment = monthlyPayment * numMonths; // Compute total interest double totalInterest = totalPayment - loanAmount; System.out.printf("Loan Amount: %8.2f - Loan period: %6d months - Annual Interest Rate: %8.2f%s \n", loanAmount, numMonths, annualInterestRate, "%"); System.out.printf("Monthly payment: %8.2f: \n", monthlyPayment); System.out.printf("Total payment: %8.2f: - total interest: %8.2f \n", totalPayment, totalInterest); } // End of main } // End of class Solution_2

You might also like