You are on page 1of 2

/**

*
*/
package PROBLEM_2;

import java.util.*;//Class Scanner

public class Program_2 {

public static void main(String[] args) {


// TODO Auto-generated method stub
//local declarations
int months;
double interest;
double monthlyInterest;
double loanAmount;

//create a scanner object


Scanner sc = new Scanner(System.in);
//prompt the user to enter the Loan Amount
System.out.println("Loan amount ");
loanAmount = sc.nextDouble();

//Check the loan amount


while (loanAmount<0){
System.out.println("Enter postive loan amount");
loanAmount = sc.nextDouble();
}

//prompt the user to enter the number of months


System.out.println("Number of Months");
months= sc.nextInt();

//Check number of months


while (months<=0){
System.out.println("Enter postive amounts of
months");
months = sc.nextInt();
}
//prompt the user to enter the Annual interest rate
System.out.println("Annual Interest rate");
interest = sc.nextDouble();

//Check Annual Interest Rate


while (interest <=0) {
System.out.println("Enter a postive interest rate");
interest = sc.nextDouble();
}

System.out.printf("Loan amount: %6.2f - Loan Period: %2d


months - Annual Interest Rate: %6.2f \n",loanAmount, months,
interest);
//turn the annual interest to monthly interest
monthlyInterest = (interest/100)/12;
double monthlypayment = loanAmount
*((monthlyInterest)/(1-Math.pow((1+monthlyInterest), (-1*months))));
double totalPayment = monthlypayment * months;
double totalInterest = totalPayment - loanAmount;
System.out.printf("monthlypayment:
%8.2f\n",monthlypayment);
System.out.printf("Total Payment and Total Interest: %8.2f
%8.2f",totalPayment, totalInterest);

}//End of Main

}//End of Program_2

You might also like