【发布时间】:2015-10-07 16:02:23
【问题描述】:
我写了一个程序来计算你需要支付的金额。用户以年为单位输入他们的本金金额、利率和时间段,然后程序给他们未来必须支付的金额。当我运行程序时,最终结果或我需要支付的金额太大或不正确。代码如下:
#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.
我不知道我做错了什么。
【问题讨论】:
-
您是乘以年数,而不是乘以年数。使用 pow() 函数。
-
同样
rate应该是double,而不是int。 -
@alk:呵呵 - 很好发现 - 现在删除了冒犯的“不”!