Click to See Complete Forum and Search --> : c++ program homework problem


joeyw99
09-12-2001, 08:40 PM
I have to write a program that figures the remaining balance for a loan. the formula they give is:

balance = payment ( 1 - (1 + i)^k-n / i)

k = payment number
payment = amount of monthly payment
i = interest rate per month (annual rate/12)
n = total number of payments to be made

The code I have is:

interest2 = interest / 12;
balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

but it doesn't work out right. i get a negative number. Any help?

------------------
Long live SFOT!

And now...your moment of Zen!

Sure you can trust the government! Just ask an Indian!

dighn
09-13-2001, 12:36 AM
these are taken from your post:
balance = payment ( 1 - (1 + i)^k-n / i)
balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

they um.. do not really correspond to each other. your code is really saying this:
balance = payment(1 - (1+i)^(k-n) / i)

try this instead:
balance = payment * (1 - pow(1 + interest2, number) - totalnum/interest2); --> more parenthesis could make the code harder to understand

------------------
Keep it brief

[This message has been edited by dighn (edited September 13, 2001).]

Zoma
09-13-2001, 01:17 AM
balance = payment ( 1 - (1 + i)^k-n / i)
k = payment number
payment = amount of monthly payment
i = interest rate per month (annual rate/12)
n = total number of payments to be made

The code I have is:

interest2 = interest / 12;
balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

but it doesn't work out right. i get a negative number. Any help?


More info is needed. Is payment negative or positive? What value are you getting for pow(...)/i? Is it greater than 1 (so 1 minus it would be negative)? Also, is it (((1 + i)^k) - n) or ((1 + i)^(k-n))? Double check that.

Fully parenthesizing the function you wrote would give:

(1 - ((1 + i)^k) - (n/i))

That doesn't seem right.

biosx
09-13-2001, 02:15 PM
Remember that when you do division that it is strong practice to make all the variables float or double (usually double).

For instance:

a = b/5;

When b = 10, a = 2.. simple, right?

Ok, how about this:

when b = 12, a = 2... what?, you say.

Remember, intergers are truncated when divided. So make sure all of your variables are double or float. Then write your division problems like so:

a = b / 12.0;

See what happens after that.

------------------
## root is the greed of all evil ##

[This message has been edited by biosx (edited September 13, 2001).]

crackfiend
09-14-2001, 12:58 PM
balance = payment ( 1 - (1 + i)^k-n / i)

This has got to be wrong. What you have (with more parenthesis for clarity) is:

balance = payment * (1 - (1+i)^k - (n/i))

Break it down:

(1+i)^k is between 1 and 2, probably very close to 1

(n/i) is a large number

so (1 - ~1 - large #) is going to be negative. You multiply that by payment, which is positive, and your answer is negative.



------------------
"On the side of the software box, in the 'System Requirements' section,
it said 'Requires Windows 95 or better'. So I installed Linux."
-Anonymous

crackfiend
09-14-2001, 01:57 PM
I just got back from lunch, took a look at it again, and I think what you're looking for is this:

balance = payment * ((1-(1+i)^(k-n))/i)

Your code is dividing by (1+i)^(k-n) by i insteaad of dividing 1-(1+i)^(k-n) by i.

Originally posted by joeyw99:
I have to write a program that figures the remaining balance for a loan. the formula they give is:

balance = payment ( 1 - (1 + i)^k-n / i)

k = payment number
payment = amount of monthly payment
i = interest rate per month (annual rate/12)
n = total number of payments to be made

The code I have is:

interest2 = interest / 12;
balance = payment * (1 - (pow((1 + interest2), (number - totalnum))) / interest2);

but it doesn't work out right. i get a negative number. Any help?





------------------
"On the side of the software box, in the 'System Requirements' section,
it said 'Requires Windows 95 or better'. So I installed Linux."
-Anonymous