C程序生成电费账单

王林
王林 转载
2023-09-17 16:09:03 940浏览

C程序生成电费账单

根据用户消耗的单位数,生成电费账单。如果消耗的单位数更多,则单位费用的费率也会增加。

应用的逻辑如果用户消耗的最低单位如下所示:

if (units < 50){
   amt = units * 3.50;
   unitcharg = 25;
}

应用的逻辑如果单位在50到100之间如下所示 −

else if (units <= 100){
   amt = 130 + ((units - 50 ) * 4.25);
   unitcharg = 35;
}

如果单位在 100 到 200 之间,则应用的逻辑如下所述 -

else if (units <= 200){
   amt = 130 + 162.50 + ((units - 100 ) * 5.26);
   unitcharg = 45;
}

应用的逻辑如果单位数量超过200如下所述 −

amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75);
unitcharg = 55;

因此,最终金额将按照以下逻辑生成 -

total= amt+ unitcharg;

示例

以下是生成电费单的 C 程序 -

现场演示

#include <stdio.h>
int main(){
   int units;
   float amt, unitcharg, total;
   printf(" Enter no of units consumed : ");
   scanf("%d", &units);
   if (units < 50){
      amt = units * 3.50;
      unitcharg = 25;
   }else if (units <= 100){
      amt = 130 + ((units - 50 ) * 4.25);
      unitcharg = 35;
   }else if (units <= 200){
      amt = 130 + 162.50 + ((units - 100 ) * 5.26);
      unitcharg = 45;
   }else{
      amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75);
      unitcharg = 55;
   }
   total= amt+ unitcharg;
   printf("electricity bill = %.2f", total);
   return 0;
}

输出

当执行上述程序时,会产生以下结果 -

Enter no of units consumed: 280
electricity bill = 1493.50

以上就是C程序生成电费账单的详细内容,更多请关注php中文网其它相关文章!

声明:本文转载于:tutorialspoint,如有侵犯,请联系admin@php.cn删除