# Thread: Problem finding the condition for loop

1. ## Problem finding the condition for loop

I'm currently studying C language and I had a doubt in one of the program questions. The question is given below :

PROBLEM
​-----------------------------------------------------------
Write a program to calculate the Loan Balance, where a person borrows an amount A and in return he/she agrees to make N payments per year, each of amount P. While the person is repaying the loan, interest will accumulate at an annual percentage rate of R, and this interest will be compounded N times a year (along with each payment). Therefore, the person must continue paying these installments of amount P until the original amount and any accumulated interest is repaid.

NOTE: The formula to calculate the amount that the person needs to repay after T years is-

Balance Amount after T years = A[(1+R/N)^NT]-P
-----------------------------------------------------------

I have a few doubts :

1. I think that the "balance amount" formula can directly give the "loan balance" for the person. I'm not sure if it's correct but in that case the question would serve no purpose. Maybe I'm wrong.

2. If there should be a loop to calculate the loan balance, what condition should I give and which loop will be better to use?

It would be really grateful if anyone could help me with this problem. I'm a beginner so please forgive me if I say something stupid.

Thank you in advance for your help!

2. Yep, that looks like a formula for compound interest. So if you put in T, you'll get out the balance amount.

I think the point is to work out how many years it'll take to pay off the loan completely. Of course, you could set balance amount to 0 in the equation and solve for T, but you'd end up with K = N^T to solve for T, with some horrible constants for K and N. Doable but not trivial (logarithms, I guess).

So, if you were doing it on pen and paper and didn't fancy delving into some maths, you'd probably try trial and error. Same deal with a program, except it can do it faster. If I've interpreted the question right, you'll want to vary the number of years in the loop, and stop the loop when the balance is less than or equal to 0.

I'd personally use a while loop, but you can use any sort of loop!

3. If you're trying to produce a table showing all balances, interests and payments, then a simple for loop with things like
balance = balance + interest;
balance = balance - payment;
will suffice.

The formula allows you to calculate the balance at a point in time, without having to calculate all the intervening steps.

But there's nothing to stop you running the formula in a loop if that's what you want.

Which you use is your choice.

4. The termination condition is when the total amount of money paid exceeds the "original amount" plus any accumulated interest. That is what the problem description says.

Any loop construct will do. Anything you can express in one type of loop construct, can be expressed in another type. They are all equivalent. The only difference between types of loops is that some are better suited to some problems than to others.

5. Thank you smokeyangel, Salem & grumpy

So the condition should be something like this :

Code:
```while(amount paid <= original amount)
{
calculate payment made at rate R
amount paid = amount paid + payment made
}```
OR

Code:
```while (balance <= 0)
{
calculate payment made at rate R for year Y
balance = balance - payment made
Y = Y - 1
}```

6. Have you been explicitly told that you must use the compound interest formula?

7. Originally Posted by Click_here
Have you been explicitly told that you must use the compound interest formula?
It is given in the question so I suppose I should use it. Is there any better alternative?

8. It's probably best to use the information given in the question.

9. Using a "for" loop is the easiest way of solving this problem, but I suspect that the marker for your assignment wants you to use the formula - Because there is a difference in amount of work, I suggest that you email/call your professor and ask if you can use a loop instead of the compound interest formula.

10. Originally Posted by Click_here
Using a "for" loop is the easiest way of solving this problem, but I suspect that the marker for your assignment wants you to use the formula - Because there is a difference in amount of work, I suggest that you email/call your professor and ask if you can use a loop instead of the compound interest formula.
I was told by my professor that the main objective of the assignment was to learn to use iterative statements. So I think the formula isn't that much important.

Just out of curiosity, could you please tell me what condition should be given for a for loop? Please let me know what do you have in mind. I would appreciate if you shared your knowledge with me.

Thank you Click_here and everyone.

11. Originally Posted by cakerusk
I was told by my professor that the main objective of the assignment was to learn to use iterative statements. So I think the formula isn't that much important.

Just out of curiosity, could you please tell me what condition should be given for a for loop? Please let me know what do you have in mind. I would appreciate if you shared your knowledge with me.

Thank you Click_here and everyone.
The best way of understanding compound interest is to set up an spreadsheet - subtract the payments, add the interest and fees. You then need to expand it down to \$0

Graph it and have a look at what it looks like - A for loop should be easy from there

12. I wouldn't use a for loop for this, but you could. Since the number of years is unknown it'd be something like:

Code:
`for (Y = 0; balance >= 0; Y++)`
I personally think a while loop is clearer. If we were iterating over a fixed number of years I'd definitely use a for loop. Is up to you.

Originally Posted by cakerusk
Code:
```while (balance <= 0)
{
calculate payment made at rate R for year Y
balance = balance - payment made
Y = Y - 1
}```
Hmm, yes, sort of but not quite. I think you want to add 1 to year not subtract 1.

If you use the formula given in the assignment, then you can calculate the balance for year Y -- meaning you don't need to subtract the payment amount. If you decide to use a loop to subtract the payments rather than recalculating using the formula, then you must also remember to calculate the interest and add the interest to the balance. I don't think there's any significant difference in the amount of work whichever way you decide to do it. You still need to find the value of Y that pays off the balance.

Popular pages Recent additions