我试图在没有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;
}
答案 0 :(得分:1)
您的计算中不是compounding the interest。
根据您的功能,interest2 += interest / 100
。
这意味着,对于10%的利息,您将拥有:
0 : 1
1 : 1.1
2 : 1.2
3 : 1.3
但在复利情况下,利息适用于以前赚取的利息以及本金:
0 : 1
1 : 1.1
2 : 1.21
3 : 1.331
尝试这样的事情:
interest2 = 1 + interest / 100.0;
totalAmount = startingAmount;
while (numberOfYears--) {
totalAmount *= interest2;
}
答案 1 :(得分:0)
非常感谢,我总是很高兴得到不同的意见,我发现它虽然有用,但我添加了这个:
double calcFutureValue(double startingAmount, double interest, int numberOfYears) {
double totalAmount;
double interest2 = 1;
double random3 = 1 + interest / 100;
for (int i = 0; i < numberOfYears; i++)
{
interest2 *= random3;
totalAmount = startingAmount * interest2;
printf("%lf", totalAmount);
getchar();
}
return totalAmount;