【发布时间】:2017-09-24 04:27:35
【问题描述】:
我试图在没有 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;
}
【问题讨论】:
标签: function input double output calculation