-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMortgage_calculator.java
33 lines (22 loc) · 1.03 KB
/
Mortgage_calculator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package com.company;
import java.text.NumberFormat;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
final byte MONTHS_IN_YEARS = 12;
final byte PERCENT = 100;
Scanner scanner = new Scanner(System.in);
System.out.print("Principal: ");
int principal = scanner.nextInt();
System.out.print("Annual Interest Rate: ");
float annualInterest = scanner.nextFloat();
float monthlyInterest = annualInterest / PERCENT / MONTHS_IN_YEARS;
System.out.print("Period (Years): ");
byte years = scanner.nextByte();
int numberOfPayments = years * MONTHS_IN_YEARS;
double mortgage = principal * (monthlyInterest * Math.pow(1 + monthlyInterest, numberOfPayments)
/ (Math.pow(1 + monthlyInterest, numberOfPayments) - 1));
String mortgageFormatted = NumberFormat.getCurrencyInstance().format(mortgage);
System.out.print("Mortgage: " + mortgageFormatted);
}
}