【发布时间】:2022-01-16 04:25:08
【问题描述】:
我正在尝试编写一个程序来打印 c 编程中 dev c++ 的单利和复利,但它没有打印正确的复利,谁能告诉我为什么? 这是我的代码:
#include<stdio.h>
#include<math.h>
int main()
{
int p,r,t,si,ci;
printf("enter the principle:");
scanf("%d",&p);
printf("enter the rate:");
scanf("%d",&r);
printf("enter the time:");
scanf("%d",&t);
si=p*r*t/100;
ci=p*pow((1+(r/100)),t);
printf("simple interest:%d",si);
printf("\ncompound interest:%d",ci);
int a=getch();
return 0;
}
【问题讨论】:
-
您似乎正在为需要浮点计算的东西进行整数计算
-
你确定所有的变量都应该是整数吗?
-
例如.. 当
r的值低于 100 时,r/100将给出结果 0(零)。可能不是您想要的。考虑改用double类型。 -
别忘了检查
scanf()的返回值,C 中没有getch(),除非你包含非标准的古代DOS 头conio.h。 (改用getchar()) -
当您更改为
double时,请使用%lf而不是%d