Thread: Problem finding the condition for loop

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    4

    Question 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. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    May 2013
    Posts
    4
    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. #6
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Have you been explicitly told that you must use the compound interest formula?
    Fact - Beethoven wrote his first symphony in C

  7. #7
    Registered User
    Join Date
    May 2013
    Posts
    4
    Quote Originally Posted by Click_here View Post
    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. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It's probably best to use the information given in the question.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    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.
    Fact - Beethoven wrote his first symphony in C

  10. #10
    Registered User
    Join Date
    May 2013
    Posts
    4
    Quote Originally Posted by Click_here View Post
    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. #11
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by cakerusk View Post
    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
    Fact - Beethoven wrote his first symphony in C

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    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.


    Quote 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 subscribe to a feed

Similar Threads

  1. do-while loop with condition
    By xenocid3r in forum C Programming
    Replies: 1
    Last Post: 03-02-2013, 12:20 PM
  2. what condition in the while loop?
    By joeman in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2010, 11:49 AM
  3. Overide for loop condition
    By erasm in forum C Programming
    Replies: 2
    Last Post: 08-11-2009, 08:08 AM
  4. need while(for(loop)) to be condition
    By thestien in forum C++ Programming
    Replies: 4
    Last Post: 10-12-2006, 08:32 AM
  5. help with loop condition
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 03-23-2002, 12:18 PM