需要一些帮助来计算按月利率 i 在 n 个月内完全摊销 L 美元贷款所需的固定每月付款 (P)。给定的公式是:P = L[i(1 + i)n]/[(1 + i)n - 1]。我写了一个代码,但它没有计算付款。我想知道是因为我将双精度类型与 int (月数)一起使用还是公式的问题?!请帮忙。
#include<stdio.h>
#include <math.h>
double calculatePayments(double rate, double loan, int payments);
int main() {
double principal, i, monthlyP;
int month;
printf ("Enter the principal amount: ");
scanf ("%f", &principal);
printf ("Enter the interest amount: ");
scanf ("%f", &i);
printf ("Enter the term in months: ");
scanf ("%d", &month);
monthlyP = calculatePayments (i, principal, month);
printf ("The monthly payment amount is %.2f: ", monthlyP);
return 0;
}
double calculatePayments(double rate, double loan, int payments) {
double mPayments;
mPayments = loan*(rate*(1 + rate)*payments)/((1 + rate)*payments - 1);
return mPayments;
}