There are several points to consider here. If a value must always be a whole number, such as the number of months, then type
int
is appropriate. For other values, where there may be decimal places involved, use type
double
instead.
(If you are really unsure, just use
double
for all the numeric variables).
As for the calculation, there are a couple if places where it's possible to go wrong. When entering the interest rate, such as 10%, the user will enter the value "10" but you need to divide by 100, such as 10/100.0 to get the proper value for the calculation. As well as that, when dealing with a monthly repayment, the interest rate should be divided by 12, so that one-twelfth of the interest is applied each month.
There's an example here, where an interest rate of 8% becomes 0.00666666666666667 in the calculation.
http://www.1728.org/loanform.htm
For the second part of the question, you need a different formula:
PV=(C/(i/12)) x (1-(1/(1+(i/12))^n))
where
C = payment
i = interest rate
n = loan term |
see this page
http://www.ehow.com/how_4947695_calculate-loan-amount-payment.html