1

我试图在没有 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;
 }
4

2 回答 2

1

您并没有增加对计算的兴趣

根据您的功能,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;
}
于 2017-04-26T16:34:17.953 回答
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;
于 2017-04-26T16:41:18.493 回答