我遇到的问题是我的代码会陷入无限循环或者会出现堆栈溢出问题,并在计算过程中开始产生负数.
我知道这个问题来自我的while循环,并且认为问题可能在于我使用的公式是线i = (r / 12)*(b - p + temp);.
但是我不确定如何解决这个问题.我的公式是试图计算12个月内每月固定利息贷款的利息,并将剩余的余额打印到屏幕上.这应该持续到平衡达到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, …Run Code Online (Sandbox Code Playgroud)