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
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP