1

The problem I've been having is that my code will get stuck either in an infinite loop or will have a stack overflow problem and begin producing negative numbers during calculations.

I am aware that this issue is coming from my while loop and also think the issue probably lies with the formula I am using being the line i = (r / 12)*(b - p + temp);.

However I am unsure of how to fix this. My formula is trying to calculate the interest paid on a fixed interest loan for each month over 12 months and print that to the screen with the remaining balance. This is supposed to continue until the balance reaches 0.

#include <stdio.h>

// main function
int main()
{
    // variable declarations
    float r = 0.22;   // interest rate
    float b = 5000.0; // amount borrowed
    float p;          // payment amount
    int   m = 1;
    float temp, ti = 0;
    float i;

    // Take in data from user
    printf("Please enter the amount you wish to pay monthly: \n");
    scanf("%f", &p);
    printf("\n");

    //display interest rate, initial balance, monthly payment
    printf("r = %.2f\nb = %.1f\np = %.1f \n\n", r, b, p);

    // Month by month table showing month interest due/paid and remaining balance
    i = (r / 12) * b;
    temp = i;
    printf("%d %.2f %.2f\n", m,i,b);
    m++;

    while (i > 0) {
        i = (r / 12) * (b - p + temp);
        b = (b - p + temp);
        ti += temp;
        temp = i;
        printf("%d %.2f %.2f\n",m, i, b);
        m++;
    }
    printf("\n");
    printf("total interest paid: %.2f\n", ti);

    return 0;
}
3
  • Please do mention what you're trying to achieve with the formula. Commented Jan 20, 2016 at 17:11
  • updated to include that information. Commented Jan 20, 2016 at 17:13
  • It would be good if you explained what the variable i is. Commented Jan 20, 2016 at 17:20

3 Answers 3

5

Well, if I'm doing the math correctly it looks like if you enter a value less than 91.67 for P you are going to get stuck in an infinite loop because the monthly payments are less than the interest accusing on the debt; so you might want to add a check for that.

As an aside if you named your variables as Interest, Base, etc. you wouldn't need the comments and the code would be easier to read.

Also since you are printing out the payment info until balance is zero you should loop while b > 0.

Sign up to request clarification or add additional context in comments.

Comments

1

The program works as expected. The only problem is, if your monthly payment are less than the interest rate - then the amount you need to pay back grows exponentially and the program never stops.

Enter any number >= 92 and it seems to work.

Is 22% p.a. interest rate correctly?

1 Comment

"The only problem is, if your monthly payment are less than the interest rate - then the amount you need to pay back grows exponentially and the program never stops." Like real life :)
1

I don't see this generating an infinite loop but it would become a problem if your repayment is higher than the matured interest, which with your starting parameter it means anything below 91.67.

You may have a wrong end condition so there is always a negative line printed, though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.