The Database Managers, Inc.

Contact The Database Managers, Inc.


Use an RSS enabled news reader to read these articles.Use an RSS enabled news reader to read these articles.

Loan Payment Calculator

by Curtis Krauskopf

There are some programs that are universal to programming -- the kind of program that everybody eventually makes. One of those programs is a loan payment calculator. As a student, you might need to calculate a monthly loan payment for a class project, or as a professional you might need to calculate it for the finance department for your business, or you might need it for yourself to find out the monthly payment for a car loan or a house loan (mortgage) or a personal loan.

The classic equation for calculating the monthly payment for a loan is:

Payment  = 
intRate * (
principal
paymentsPerYear
)

1 -  
intRate
paymentsPerYear
 + 1
-(paymentsPerYear * numYears)

where...

Payment
is the monthly payment
intRate
is the interest rate. A 12% interest rate is 0.120.
principle
is the amount of money being loaned.
paymentsPerYear
is the number of payments per year. For a monthly payment, this will be 12.
numYears
is the number of years that the loan will be paid back.

In the equation, (paymentsPerYear * numYears) is the total number of payments for the loan. If the loan has an unusual number of payments, such as 16, that number can be substituted for (paymentsPerYear * numYears).

Similarly, the Payment is assumed to be made monthly (paymentsPerYear is 12). If payments are made weekly, quarterly or for any other timeframe, simply use the number of payments per week, quarter or any other timeframe and the Payment amount will adjust accordingly. For example, to make payments quarterly, set paymentsPerYear to 4 and the equation will still work.

The following example program implements the above equation to calculate the monthly payment for a user-specified principle, interest rate and number of payments.

To use it, compile it and then run it:

dfcomp FINANCE.src
dfrun FINANCE

Download finance.src (2k)

/LoanCalculator

       Loan Payment Calculator

           Principle:  _______.__ (amount of money being loaned)
       Interest rate:  ___.____   (12% = 12)
  Number of payments:  ____.      (15 years of monthly payments = 180)
                                  (30 years of monthly payments = 360)

     Monthly Payment:  _______.____


           Press ESC to quit

/*
// Copyright (c) 2005 by Curtis Krauskopf
//                       >http://www.decompile.com
// This is a DataFlex character-mode style program.
// Permission to use, copy, modify, distribute and sell this software for any
//     purpose is hereby granted without fee, provided that the above copyright
//     notice appears in all copies of this file and in all modified versions
//     of this file.
// The author makes no representations about the suitability of this
//     software for any purpose. It is provided "as is" without express
//     or implied warranty.
// Created:  February 19, 2005 by Curtis Krauskopf (cdk)
//                                >http://www.decompile.com
//
// This program calculates the monthly payment for a loan.
// When the user provides the amount of money being loaned (the principle),
// the interest rate and the number of payments, the program calculates
// the monthly payment amount that will pay off the loan in the number
// of months specified by the user.
//

name nPrinciple
name nInterestRate
name iPayments
name nMonthlyPayment

repeat
  number numerator
  number denominator
  number base exponent
  number intRate
  number paymentsPerYear

  accept nPrinciple
  accept nInterestRate
  accept iPayments
 
  move 12 to paymentsPerYear    
  movenum (nInterestRate / 100) to intRate
  movenum (intRate * nPrinciple / paymentsPerYear) to numerator
  movenum (-1 * paymentsPerYear * (iPayments / number(12))) to exponent
  movenum ((intRate / paymentsPerYear) + 1.0) to base
  movenum (1.0 - (base ^ exponent)) to denominator
  movenum (numerator / denominator) to nMonthlyPayment
loop

keyproc key.escape
abort

when the program runs, it looks like this:


       Loan Payment Calculator

           Principle:  __________
       Interest rate:  ________   (12% = 12)
  Number of payments:  _____      (15 years of monthly payments = 180)
                                  (30 years of monthly payments = 360)

     Monthly Payment:  ____________


           Press ESC to quit


This article was written by Curtis Krauskopf (email at ).

Curtis Krauskopf is a software engineer and the president of The Database Managers (www.decompile.com). He has been writing code professionally for over 25 years. His prior projects include multiple web e-commerce applications, decompilers for the DataFlex language, aircraft simulators, an automated Y2K conversion program for over 3,000,000 compiled DataFlex programs, and inventory control projects. Curtis has spoken at many domestic and international DataFlex developer conferences and has been published in FlexLines Online, JavaPro Magazine, C/C++ Users Journal and C++ Builder Developer's Journal.

The Database Managers helps companies to:
  •  become more profitable
  •  grow their business
  •  fix programs that are behaving badly
  •  write new programs to solve business problems
  • do more with fewer resources
Email them at to find out how to make your company more successful.

Copyright 2003-2010 The Database Managers, Inc.

 

DataFlex: Tips | Freeware | WebApp Server | Books | Links
Services | Programming | Contact Us | Recent Updates

Send feedback to: