C Language

C code to calculate Electricity bill | C example

C code to calculate Electricity bill

In this article, we will discuss the concept of C code to calculate Electricity bill

In this program, we are going to learn how to calculate electricity bill using different methods in C language.

This is done using if else-if  , function, in C language

Program to calculate Electricity bill

Program to calculate Electricity bill using if else-if

In this program, we will calculate electricity bill using if else if with && operator in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int unit; //declare variable unit

    //first we understand unit prize
    /*1 - 100 unit - 5/=
      101-200 unit -  7/=
      201-300 unit - 10/=
      above 300  - 15/=*/      printf("Enter your usage: ");
      scanf("%d",&unit);

      if(unit>0 && unit<=100){//when this statement is true
            printf("Bill amount is: ");
  printf("%d",unit*5);//this statement is Executed otherwise
}
else if(unit>100 && unit<=200){//when this statement is true
        printf("Bill amount is: ");
  printf("%d",(100*5)+(unit-100)*7);//this statement is Executed otherwise
}
else if(unit >200 && unit<=300){//when this statement is true
    printf("Bill amount is: ");
  printf("%d",(100*5)+(100*7)+(unit-200)*10);//this statement is Executed otherwise
}
else if(unit >300){//when all statements are false
    printf("Bill amount is: ");//consumed above 30 units
  printf("%d",(100*5)+(100*7)+(100*10)+(unit-300)*15);
  //this statement is Executed otherwise
}
else{
    printf("No charges");
}
getch();
    return 0;
}

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

Enter your usage: 340
Bill amount is: 3700

 

 

Program to Calculate Electricity bill using function

In this program, we will calculate electricity bill using function in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    double calcBill(int a);
    int unit; //declare variable unit

    //first we understand unit prize
    /*1 - 100 unit - 5/=
      101-200 unit -  7/=
      201-300 unit - 10/=
      above 300  - 15/=*/      printf("Enter your usage of unit: ");
      scanf("%d",&unit);
      double total=calcBill(unit);

getch();
    return 0;
}
double calcBill(int unit){//function dfinition
      if(unit<=100){//when this statement is true
            printf("Bill amount is: ");
  printf("%d",unit*5);//this statement is Executed otherwise
}
else if(unit<=200){//when this statement is true
        printf("Bill amount is: ");
  printf("%d",(100*5)+(unit-100)*7);//this statement is Executed otherwise
}
else if(unit<=300){//when this statement is true
    printf("Bill amount is: ");
  printf("%d",(100*5)+(100*7)+(unit-200)*10);//this statement is Executed otherwise
}
else{//when all statements are false
    printf("Bill amount is: ");
  printf("%d",(100*5)+(100*7)+(100*10)+(unit-300)*15);
  //Finally,this statement is Executed
}
}

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

Enter your usage of unit: 340
Bill amount is: 2800

 

Program to Calculate Electricity bill using function-another way

In this program, we will calculate electricity bill using function in C language

Program 3

#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);
    calc_Electricity(unit);//function call
    getch();
    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. %.3f",amount);

}

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

Enter total number of unit consumed:
350
Electricity bill=Rs. 975.00

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function in C language

 

Similar post

Python code to calculate Electricity bill

C++ code to calculate Electricity bill

C code to calculate Electricity bill

Java code to calculate Electricity bill

 

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Karmehavannan

Recent Posts

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

11 months ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

11 months ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

11 months ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

11 months ago

Function/method to convert Celsius into Fahrenheit -Entered by user

Function/method to convert Celsius into Fahrenheit -Entered by user In this article, we will discuss…

11 months ago

Temperature conversion: Celsius into Fahrenheit using function or method

Temperature conversion: Celsius into Fahrenheit using a function or method In this article, we will…

11 months ago

This website uses cookies.