我试图在没有 math.h 和 pow 的情况下计算租金,不知何故我几乎做对了,但它没有计算正确的金额,我不确定问题可能出在哪里,关于我缺少什么的任何建议?
#include <stdio.h>
double calcFutureValue(double startingAmount, double interest, int numberOfYears);
int main() {
double startMoney, interest, futureMoney;
int years;
printf("Enter amount of money: ");
scanf("%lf", &startMoney);
getchar();
printf("Enter interest on your money: ");
scanf("%lf", &interest);
getchar();
printf("Enter amount of years: ");
scanf("%d", &years);
getchar();
futureMoney = calcFutureValue(startMoney, interest, years);
printf("In %d years you will have %.1f", years, futureMoney);
getchar();
return 0;
}
double calcFutureValue(double startingAmount, double interest, int numberOfYears) {
double totalAmount;
double interest2 = 1;
for (int i = 0; i < numberOfYears; i++)
{
interest2 += interest / 100;
totalAmount = startingAmount * interest2;
printf("%lf", totalAmount);
getchar();
}
return totalAmount;
}