Bill calculation

C program to calculate electricity bill

C program to calculate electricity bill

In this tutorial, we will discuss C program to calculate electricity bill

In this post, we will learn how to calculate electricity bill using if condition in the C programming language

We can calculate monthly electric power usage in many ways.

In this tutorial, we display two programs calculating electricity bill in Java.

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

/*1 - 100 unit - 5/= 
101-200 unit - 7/= 
201-300 unit - 10/= 
above 300 - 15/=*///for example

Example

C program to calculate electricity bill

We can explain of this program as follows

  • Declare total unit consumed by the customer using the variable unit.
  • When the unit consumed less or equal to 100 units, calculates the total amount of usage =unit*5
  • When unit consumed between 100 to 200 units, calculates the total amount of usage =(100*5)+(unit-100)*7)
  • When unit consumed between 200 to 300 units ,calculates total amount of usage =100*5)+(100*7)+(unit-200)*10
  • When unit consumed above 300 units ,calculates total amount of usage =(100*5)+(100*7)+(100*10)+(unit-300)*15
  • No additional charge

Electricity bill calculation – using normal method

program 1

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

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

    //first we understand unit prize
    /*1 - 100 unit - 5/=
      101-200 unit -  7/=
      201-300 unit - 10/=
      above 300  - 15/=*/
      if(unit<=100){//when this statement is true
    printf("%d",unit*5);//this statement is Executed otherwise
}
else if(unit<=200){//when this statement is true
    printf("%d",(100*5)+(unit-100)*7);//this statement is Executed otherwise
}
else if(unit<=300){//when this statement is true
    printf("%d",(100*5)+(100*7)+(unit-200)*10);//this statement is Executed otherwise
}
else if(unit>300){//when this statement is true
    printf("%d",(100*5)+(100*7)+(100*10)+(unit-300)*15);//this statement is Executed otherwise
}
else{//when all statements are false
    printf("No value");//this statement is Executed
}
getch();
    return 0;
}

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

2365

 

Electricity bill calculation – using take input from the user

Program 2

#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 total units consumed: ");
      scanf("%d",&unit);

      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: ");//consumed above 30 units
    printf("%d",(100*5)+(100*7)+(100*10)+(unit-300)*15);
    //this statement is Executed otherwise
}
getch();
    return 0;
}

 

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

Enter total units consumed: 235
Bill amount is: 1550

 

Electricity bill calculation – using Using & operator

#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/=
for example*/      printf("Enter total units consumed: ");
      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{//all statements are false
    printf("No charges");
}//this statement is executed
getch();
    return 0;
}

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

Enter total units consumed: 250
Bill amount is: 1700

 

Suggested for you

Operator in C language

If statements in C language

Function in C language

Java code to calculate electricity bill
Cpp program to calculate electricity bill
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

Recent Posts

PHP Star Triangle pattern program

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

1 month 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