我编写了一个程序来计算您需要支付的金额。用户输入他们的本金金额、利率和以年为单位的时间段,然后程序会告诉他们 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/