【问题标题】:compound interest program problem in dev c++ using c使用c的dev c ++中的复利程序问题
【发布时间】: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

标签: c dev-c++


【解决方案1】:

你给出的是 int 而不是 float。

#include<stdio.h>
#include<math.h>
int main(){
    float 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;
}

【讨论】:

    猜你喜欢
    • 2023-03-31
    • 1970-01-01
    • 2021-02-21
    • 2011-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多