[3 / 1 / 3]
Quoted By: >>1264309
I'm doing a project where I have to write a program to calculate the monthly payment on a mortgage but I cant figure out the last few questions
Use the printf method to print the monthly payment, rounded to cents.
Use the printf method to print the total payment, rounded to dollars.
Use the printf method to print the total interest payment, rounded to dollars.
I tried System.out.printf("%f.2%n", monthlyPayment); to convert the monthly payment to cents but instead of converting it to cents it just rounded the number down
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("enter the mortgage principal in dollars as an integer: ");
int p = scan.nextInt();
System.out.print("enter the length of the mortgage in years as an integer: ");
int years = scan.nextInt();
System.out.print("enter the annual interest rate in percentage as a double value: ");
double annualRate = scan.nextDouble();
int n = years * 12;
double r = annualRate / 1200;
double payment = (p * r * Math.pow(1+r, n)) / (Math.pow(1+r, n)-1);
double totalPayment = payment * n;
double totalInterestPayment = totalPayment - p;
System.out.printf("\nmonthly payment: $%.2f\n", payment);
System.out.printf("total payment of the mortgage: $%.0f\n", totalPayment);
System.out.printf("total interest payment: $%.0f\n", totalInterestPayment);
System.out.printf("Monthly Payment in cents: %f.2%n", payment);
}
}
Thanks!
Use the printf method to print the monthly payment, rounded to cents.
Use the printf method to print the total payment, rounded to dollars.
Use the printf method to print the total interest payment, rounded to dollars.
I tried System.out.printf("%f.2%n", monthlyPayment); to convert the monthly payment to cents but instead of converting it to cents it just rounded the number down
import java.util.Scanner;
public class main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.print("enter the mortgage principal in dollars as an integer: ");
int p = scan.nextInt();
System.out.print("enter the length of the mortgage in years as an integer: ");
int years = scan.nextInt();
System.out.print("enter the annual interest rate in percentage as a double value: ");
double annualRate = scan.nextDouble();
int n = years * 12;
double r = annualRate / 1200;
double payment = (p * r * Math.pow(1+r, n)) / (Math.pow(1+r, n)-1);
double totalPayment = payment * n;
double totalInterestPayment = totalPayment - p;
System.out.printf("\nmonthly payment: $%.2f\n", payment);
System.out.printf("total payment of the mortgage: $%.0f\n", totalPayment);
System.out.printf("total interest payment: $%.0f\n", totalInterestPayment);
System.out.printf("Monthly Payment in cents: %f.2%n", payment);
}
}
Thanks!