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


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

where:
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!

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();
currency.format(someValue);

http://download.oracle.com/javase/1.4.2/docs/api/java/text/NumberFormat.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.