Generate an electricity bill based on the number of units consumed by the user. If more units are consumed, the rate per unit charge also increases.
Applied logicIf the user consumes the lowest unitis as follows:
if (units < 50){ amt = units * 3.50; unitcharg = 25; }
Applied logicIf the unit is between 50 and 100is as follows As shown −
else if (units <= 100){ amt = 130 + ((units - 50 ) * 4.25); unitcharg = 35; }
If the units are between 100 and 200, the logic applied is as follows-
else if (units <= 200){ amt = 130 + 162.50 + ((units - 100 ) * 5.26); unitcharg = 45; }
The logic appliedIf the number of units exceeds 200 is as follows As stated −
amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75); unitcharg = 55;
Therefore, the final amount will be generated as per the following logic-
total= amt+ unitcharg;
The following is the C program to generate the electricity bill-
Live demonstration
#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; }
When the above program is executed, the following results are produced-
Enter no of units consumed: 280 electricity bill = 1493.50
The above is the detailed content of C program to generate electricity bill. For more information, please follow other related articles on the PHP Chinese website!