Comparing loans

Started by
3 comments, last by toogreat4u 14 years, 1 month ago
I am trying to figure out where I have screwed up my algorithm for comparing loans. The program I am writing is just a simple java program thats main focus is on learning how to write correct loops. Anyhow the book shows certain numbers for the answers and I am not getting them. Here is my code:

package ProgrammingExercise4_21;

/* (Financial application: comparing loans with various interest rates) Write a program
 * that lets the user enter the loan amount and loan period in number of years
 * and displays the monthly and total payments for each interest rate starting from
 * 5% to 8%, with an increment of 1/8. Suppose you enter the loan amount 10,000
 * for five years; display a table as follows: page 135.
 */

public class CompareLoans {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		final double LOAN_AMOUNT = 10000;
		final int NUMBER_OF_YEARS = 5;
		double interestRate = 5.0;
		
		System.out.println("Loan Amount: " + LOAN_AMOUNT);
		System.out.println("Number of Years: " + NUMBER_OF_YEARS);
		System.out.println("Interest Rate\tMonthly Payment\tTotal Payment");
		
		for(; interestRate <= 8.0; interestRate = interestRate + (1.0/8.0)) {
			System.out.print(interestRate + "%\t");
			
			double tempLoanAmount = LOAN_AMOUNT;
			double totalPayment = 0;
			
			for(int monthsLeft = NUMBER_OF_YEARS*12; monthsLeft > 0; monthsLeft--) {
				double payment = tempLoanAmount / monthsLeft;
				totalPayment += payment;
				tempLoanAmount -= payment;
				
				if((monthsLeft != NUMBER_OF_YEARS*12) && (monthsLeft % 12 == 0)) {
					tempLoanAmount = tempLoanAmount + (tempLoanAmount * (interestRate / 100));
				}
				
			}
			
			double monthlyPayment = totalPayment / (NUMBER_OF_YEARS*12);
			System.out.printf("%14.2f\t%16.2f\n", monthlyPayment, totalPayment);
		}
	}

}

The book is showing these numbers: 5% 188.71 11322.74 5.125% 189.28 11357.13 5.25% 189.85 11391.59 .... 7.85% 202.16 12129.97 8.0% 202.76 12165.83 I feel foolish that I can't do simple loan algorithms but I am annoyed that my numbers are incorrect. Any help is greatly appreciated. Thank you.
Advertisement
Edit: I don't really understand where these numbers are coming from. If you have an interest rate of 5% and a loan duration of 5 years, using simple interest the total amount to pay would be $102500.00. Then you would just divide that amount by the duration of the loan in months to get the monthly payment.

Maybe you can clarify how the interest rate is supposed to be applied in this question?

[Edited by - X Abstract X on March 11, 2010 11:39:39 PM]
Quote:The book is showing these numbers
And what numbers are you getting?

Quote:I feel foolish that I can't do simple loan algorithms but I am annoyed that my numbers are incorrect.
Ok, but so what? Everyone makes mistakes. Smart programmers aren't ones that are perfect. They are ones that can break down problems. If there is a bug in the code, good programmers can debug effectively. So what have you tried?
Your formula is a tad off. Their looking for a monthly payment. Unfortunately, accountants are a few eggshells past being cracked and there's as many ways to figure it as there are egos writing them.

Fortunately, the one you're looking for has been in programming books since punch cards.

iRate = Interest Rate
ppy = payments per year

payment = (iRate * (principal / ppy))/ (1 - ((iRate / ppy) + 1)^-(ppy * numyears))

I don't use java much, but here's a quick break down in cpp.

#include <iostream>#include <cmath>using namespace std;int main(){	double principle = 10000;	double Rate = 5;	double numyears = 5;	double iRate, num, exp, base, denom, payments, time, total;	// convert apr	iRate = Rate / 100;	// convert time	time = numyears * 12;	//figure top	num = iRate * principle / 12;	// figure the exponent portion	exp = -1 * 12 * (time / 12);	// figure a base	base = iRate / 12 + 1.0;	// complete the denominator	denom = 1.0 - pow(base, exp);	// figure payments	payments = num/denom;	// total loan	total = payments * time;	cout.precision(8);	cout << "Interest Rate\t" << "Monthly Payments\t" << "Total Loan" << endl;	cout << Rate << "%\t\t" << payments << "\t\t" << total << endl;	return 0;}


A bit messy, but it breaks the formula up enough you should be able to understand it.
Thanks GroggyOne, that helps me a lot! All comments appreciated. Thanks again.

This topic is closed to new replies.

Advertisement