
根据用户消耗的单位数,生成电费账单。如果消耗的单位数更多,则单位费用的费率也会增加。
应用的逻辑如果用户消耗的最低单位如下所示:
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中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号