Home > Backend Development > C++ > body text

C program to generate electricity bill

王林
Release: 2023-09-17 16:09:03
forward
1474 people have browsed it

C program to generate electricity bill

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;
}
Copy after login

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;
}
Copy after login

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;
}
Copy after login

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;
Copy after login

Therefore, the final amount will be generated as per the following logic-

total= amt+ unitcharg;
Copy after login

Example

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;
}
Copy after login

Output

When the above program is executed, the following results are produced-

Enter no of units consumed: 280
electricity bill = 1493.50
Copy after login

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!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template