w3resource

C Exercises: Calculate Electricity Bill

C Conditional Statement: Exercise-19 with Solution

Write a program in C to calculate and print the electricity bill of a given customer. The customer ID, name, and unit consumed by the user should be captured from the keyboard to display the total amount to be paid to the customer.
The charge are as follow :

Unit Charge/unit
upto 199 @1.20
200 and above but less than 400 @1.50
400 and above but less than 600 @1.80
600 and above @2.00

If bill exceeds Rs. 400 then a surcharge of 15% will be charged and the minimum bill should be of Rs. 100/-

Sample Solution:

C Code:

#include <stdio.h>  // Include the standard input/output header file.
#include <string.h>  // Include the string header file.

void main()
{
    int custid, conu;  // Declare variables to store customer ID and consumed units.
    float chg, surchg = 0, gramt, netamt;  // Declare variables for charge, surcharge, gross amount, and net amount.
    char connm[25];  // Declare a character array to store customer name.

    printf("Input Customer ID :");  // Prompt user for input of customer ID.
    scanf("%d", &custid);  // Read and store the customer ID.
    printf("Input the name of the customer :");  // Prompt user for input of customer name.
    scanf("%s", connm);  // Read and store the customer name.
    printf("Input the unit consumed by the customer : ");  // Prompt user for input of consumed units.
    scanf("%d", &conu);  // Read and store the consumed units.

    if (conu < 200)
        chg = 1.20;  // Set charge based on consumed units.
    else if (conu >= 200 && conu < 400)
        chg = 1.50;  // Set charge based on consumed units.
    else if (conu >= 400 && conu < 600)
        chg = 1.80;  // Set charge based on consumed units.
    else
        chg = 2.00;  // Set charge based on consumed units.

    gramt = conu * chg;  // Calculate gross amount.

    if (gramt > 300)
        surchg = gramt * 15 / 100.0;  // Calculate surcharge if gross amount is greater than 300.

    netamt = gramt + surchg;  // Calculate net amount.

    if (netamt < 100)
        netamt = 100;  // Set minimum net amount to 100.

    printf("\nElectricity Bill\n");
    printf("Customer IDNO                       :%d\n", custid);
    printf("Customer Name                       :%s\n", connm);
    printf("unit Consumed                       :%d\n", conu);
    printf("Amount Charges @Rs. %4.2f  per unit :%8.2f\n", chg, gramt);
    printf("Surchage Amount                     :%8.2f\n", surchg);
    printf("Net Amount Paid By the Customer     :%8.2f\n", netamt);
}  
 

Sample Output:

Input Customer ID :10001                                                                                      
Input the name of the customer :James                                                                         
Input the unit consumed by the customer : 800                                                                 
                                                                                                              
Electricity Bill                                                                                              
Customer IDNO                       :10001                                                                    
Customer Name                       :James                                                                    
unit Consumed                       :800                                                                      
Amount Charges @Rs. 2.00  per unit : 1600.00                                                                  
Surchage Amount                     :  240.00                                                                 
Net Amount Paid By the Customer     : 1840.00 

Flowchart:

Flowchart: Calculate Electricity Bill

C Programming Code Editor:

Previous: Write a C program to calculate profit and loss on a transaction.
Next: Write a program in C to accept a grade and display the equivalent description

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.