Calculations

C program to Electricity bill calculation using function

C program to Electricity bill calculation using function

In this tutorial, we will discuss the C program to Electricity bill calculation using the function

In this article, we will discuss how to calculate the electricity bill in the C programming language using the function

We can calculate monthly electric power usage in many ways in the C programming language

In this tutorial, I have written two programs for calculating electricity bill in C language using the function.

calculate electricity bill in C language

Before we write a program to calculate the electricity bill we must understand electricity charges and rates.

/*1 - 100 unit - 1.5/= 
101-200 unit - 2/= 
201-300 unit - 3/= 
above 300 - 4/=*/

 

In my previous post, I have explained  to the C program to calculate  the electricity bill

However, in this program, we embed the logic of the Calculate electricity bill program in the function

Once this program received the units and it will calculate the electricity bill and cost per month

After receiving the input from the user, it is stored in the variable of the unit and then program calculate electricity bill

 

This program can have the following common procedures for completion of this program

  • Declare total unit consumed by the customer using the variable unit.
  • If the unit consumed less or equal to 50 units, calculates the total amount of usage =unit*1.50
  • If the unit consumed between 50 to 150 units, calculates the total amount of usage =(50*1.5)+(unit-50)*2)
  • If  the unit consumed between 150 to 250 units ,calculates total amount of usage =(50*1.5)+(100*2)+(unit-150)*3
  • If the unit consumed above 250 units ,calculates total amount of usage =(50*1.5)+(100*2)+(100)*3+(unit-250)*4
  • No additional charge

Calculate electricity bill without && operator

Program 1

#include <stdio.h>
#include <stdlib.h>
int calc_Electricity();//function prototype
int main()
{
    //rates apply
    // 1- 50 units - 1.50
    //51- 150 units - 2.00
    //101 - 250 units - 3.00
    //above 251  units - 4.00
    int unit=0;

    printf("Enter total mun its consumed\n");
    scanf("%d",&unit);//Reads input from user and store in variable unit
    calc_Electricity(unit);//function call
    return 0;
}
int calc_Electricity(int unit){//function definition
    double amount;
    if(unit<=50)
    {//below 50 units
        amount=unit*1.50;

    }
    else if(unit<=150)
    {//below 150 units
        amount=((50*1.5)+(unit-50)*2.00);
    }
    else if((unit<=250)){//below 250 units
        amount=(50*1.5)+((150-50)*2.00)+(unit-150)*3.00;
    }
    else{//above 250 units
        amount=(50*1.5)+((150-50)*2.00)+((250-150)*3.00)+(unit-250)*4;

    }
    printf("Electricity bill = Rs. %.2f",amount);
      //display the output on the screen
}

When the above code is executed, it produces the following results

Enter total units consumed
275
Electricity bill = Rs.675.00

 

Program 2

Calculate electricity bill with && operator

#include <stdio.h>
#include <stdlib.h>
int calc_Electricity();//function prototype
int main()
{
    //rates apply
    // 1- 50 units - 1.50
    //51- 150 units - 2.00
    //101 - 250 units - 3.00
    //above 251  units - 4.00
    int unit=0;

    printf("Enter total units consumed\n");
    scanf("%d",&unit);//Reads input from user and store in variable unit
    calc_Electricity(unit);//function call
    return 0;
}
int calc_Electricity(int unit){//function definition
    double amount;
    if((unit>=1)&&(unit<=50))//between 1 - 50 units
    {
        amount=unit*1.50;

    }
    else if((unit>50)&&(unit<=150))//between 50  150 units
    {
        amount=((50*1.5)+(unit-50)*2.00);
    }
    else if((unit>150)&&(unit<=250)){//between 150 -  250 units
        amount=(50*1.5)+((150-50)*2.00)+(unit-150)*3.00;
    }
    else if(unit>250){//above 250 units
        amount=(50*1.5)+((150-50)*2.00)+((250-150)*3.00)+(unit-250)*4;

    }
    else{
            printf("No usage ");
        amount=0;
    }
    printf("Electricity bill = Rs. %.2f",amount);

}

When the above code is executed, it produces the following results

Enter total units consumed
255
Electricity bill = Rs.595.00

 

Similar post

C program to calculate electricity bill

 

Suggested for you

Operator in C language

If statement in C language

scanf()  print() function

Function in C language

User define function in C language

 

Electricity bill Calculation program using function in Cpp
Calculate electricity power:Python program using function
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

View Comments

  • Write a C program using BREAK and looping statement to compute bill payment of
    electrical consumption for different users. In order to promote saving, Tenaga
    National Berhad (TNB) charge a lower rate to users with lower consumption as
    shown in Table

Recent Posts

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago