C 程序显示不正确的结果

标签 c math

我编写了一个程序来计算您需要支付的金额。用户输入他们的本金金额、利率和以年为单位的时间段,然后程序会告诉他们 future 必须支付的金额。当我运行程序时,最终结果或我需要支付的金额太大或不正确。这是代码:

#include <stdio.h>
int calculation_1(int principal, int rate, int years);

int main(void) {

    int amount1, amount3;
    double amount2, total;

    printf("Investement Calculator \n");
    printf("====================== \n");

    printf("Principal : ");
    scanf("%d", &amount1);

    printf("Annual Rate: ");
    scanf("%lf", &amount2);

    printf("No of Years: ");
    scanf("%d", &amount3);

    total = calculation_1(amount1, amount2, amount3);

    printf("The future value is: $%.2f \n", total);

    return 0;
}

int calculation_1(int principal, int rate, int years) {

    double subtotal,final;

    subtotal = principal * (1 + rate) * years;

    return subtotal;
}

我使用这些值进行了测试:本金为 1000,利率为 0.06,年数为 5 年。最终结果应该是 1338.23 美元,但我得到了 5000.00 美元。这是我用来计算金额的公式:

total = principal * (1 + rate) ^number of years.

我不知道我做错了什么。

最佳答案

您的公式没有正确指定:

subtotal = principal * (1 + rate) * years;

应该是

subtotal = principal * pow((1 + rate),years);

另请参阅其他答案 (@ameycu)。您的数据类型不匹配。

关于C 程序显示不正确的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32997110/

相关文章:

C - 战舰,随机放置

C:`打开调试信息

algorithm - 如何调整具有纵横比的旋转矩形的大小?

java - `n` 一袋沙子并插入盒子,算法

math - 由2个角度定义的3D矢量

c - 从套接字读取直到某个字符在缓冲区中

c - 我的跳过列表真的是在 N 而不是 log(N) 中搜索吗?

c - 新线程中的服务器套接字

java - 如何应用牛顿拉夫森法求五次函数的根

c - C中的指针算术和 "generic"