Need help using #include <cmath> in C++?


I have to create the following program in C++:

[1] Calculate monthly payments for a loan
[0] Exit Program

****When i enter 1 this is what shows**
Enter the amount of the loan you would like to take out : 3500 (user input)
Enter the amount of interest : 10 (user input)
Enter the number of months you will have to pay it off : 24 (user input)
The monthly payment of your loan will be : 161.51

I found that the formula for what I need to do is the following:
P = (Pv*R) / [1 - (1 + R)^(-n)]
where
Pv = Present Value (beginning value or amount of loan)
APR = Annual Percentage Rate (one year time period)
R = Periodic Interest Rate = APR/ # of interest periods per year
P = Monthly Payment
n = # of interest periods for overall time period (i.e., interest
periods per year * number of years)

According to my example:
Pv = 3500
APR = 10
R = 10/1 (annual interest rate)
n = 2 (24 months = 2 years)
P= my answer

How do i add the formula to my code because every time i execute the code and press 1 nothing happens. the math operation just doesnt take place.Another problem Im having is that Im getting squiggly lines in the formula:
I get a squiggly line under the last parenthesis that is between the "n" and the last bracket "]"
P = (Pv*R) / [1 - (1 + R)^(-n)]
and i get a squiggly line under the first "1" in the formula.
help please.
Square brackets like this [] normally specify an element of an array. If you just want to show precedence, use ordinary curved brackets ()

The ^ operator is used in bitwise operations. If you want to raise a number to a power, use the pow() function, for example pow(x,2) instead of x^2

http://www.cplusplus.com/reference/cmath/pow/
http://www.cplusplus.com/doc/tutorial/operators/
so would it be something like this .......P = (Pv*R/(1- pow(1+R,-n)); ? (I feel like i have tried every which way but it just doesnt add up correctly)
another question would be if I have to declare them double or do I leave them int? ( i.e. double Pv, R, n)

Now i configured it so that an operation takes place but the operation is very incorrect

sry i have so many questions its just this is difficult for me, its just i have to also reverse the operation now example:
my program has to execute like this:

[1] Calculate monthly payments for a loan
[2] Calculate how much you can pay
[0] Exit Program

****When i enter 1 this is what shows**
Enter the amount of the loan you would like to take out : 3500 (user input)
Enter the amount of interest : 10 (user input)
Enter the number of months you will have to pay it off : 24 (user input)
The monthly payment of your loan will be : 161.51

*****NOW**** when i enter 2 this is whats supposed to show:
Enter the amount you can pay monthly : 161.51 (user input)
Enter the amount of interest : 10 (user input)
Enter the number of months you will have to pay it off : 24 (user input)
You are approved for : 3500.07

********how would i code the program so that it accepts decimal values like in the example above?*******

So in the second example i have to write the formula in reverse (more or less)
How is this even possible? O_o
Last edited on
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

Topic archived. No new replies allowed.