Will P. Will P. - 1 year ago 110
Java Question

Monthly payment input calculator

I'm pretty new to java, and I'm trying to write a program that will give me the monthly payments, interest total and total amount paid on a bank loan, but I believe either my math is wrong or is incorrectly formatted, because when I run it I get numbers in the negatives, and payments that I know are wrong. Could you point out where I have made the mistake?

Example :

7500 (amount borrowed)
14.5 (loan rate)
3 (# of years)

The expected output would be

258.16 (monthly payment)
1793.66 (interest paid)
9293.66 (total paid).

Code :

import java.io.*;
import java.util.*;
public class Prog58i
public static void main(String args[])
Scanner numberReader = new Scanner(System.in);
System.out.print("The amount I wish to borrow is? ");
int p = numberReader.nextInt();
System.out.print("The loan rate I can get is? ");
double r = numberReader.nextDouble();
System.out.print("How mny years will it take me to pay off the loan? ");
int m = (numberReader.nextInt())*12;

double MP = (1 +(r/1200));
MP = Math.pow(MP, m);
double payment = p *(r/1200) * (MP/(MP-1));
payment = (int)(m * 100+0.5)/100.0;
double total = (int)((m * payment)*100)/100.0;
double intetotal = (int)((total - p)*100)/100.0;

System.out.println("My monthly payments will be " + payment);
System.out.println("Total Interest Paid is " + intetotal);
System.out.println("Total amount paid is " + total);

Answer Source

According to your formula, this statement seems to be wrong

double MP = (1 + (r / 1200));

MP = Math.pow(MP, m);

The power is only on (r / 1200) not on (1 + (r / 1200))

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download