The monthly payment on a loan can be calculated using the following formula:

amount * R
monthly payment = --------------------
1 - (1 + R )

amount is the amount of money borrowed
R is the monthly rate of interest for the loan
N is the number of months to repay the loan

Write a program that allows the user calculate the monthly payment on a loan.
Your program should ask the user to enter the amount of the loan, the annual
interest rate for the loan and the length of the loan in years.

The program should then calculate and display the amount of the monthly payment.

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

public class Assignment1
  public static void main(String[] args)

double monthlypayment = 0;
     Scanner input = new Scanner(System.in);
     System.out.print("Enter the amount of money borrowed: ");
     double amount = input.nextDouble();
     System.out.print("Enter the monthly rate of interest: ");
     double R = input.nextDouble();
     System.out.print("Enter the number of years to repay the loan: ");
     double N= input.nextDouble();

N /= 12;
R *= 0.01;

monthlypayment = amount * R / 1 - Math.pow(1 + R, -N);

     System.out.println("The monthly payment will be: " + monthlypayment);


The problem I am having is I am getting huge amounts for the repayment and I cant seem to locate the problem. Any help is much appreciated!

6 Years
Discussion Span
Last Post by Akill10

The reason the RHS of your decimal point is so large is because a double is a precise type. So you need to format the output. I suggest using the getCurrencyInstance() method of the NumberFormat class which will return a currency format, then pass the value into that format.

NumberFormat currency = NumberFormat.getCurrencyInstance();


Edited by Akill10: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.