Table of Contents
C++ code to calculate Electricity bill | C++ example
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 , class and object in C++ language
Program to calculate Electricity bill
Calculate Electricity bill using if else if
In this program, we will calculate electricity bill using if else-if statements in C++ language
Program 1
//C++ code to calculate Electricity bill | C++ example #include <iostream> #include <conio.h> using namespace std; 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/=*/ cout<<"Enter the unit of your usage: "; //ask input from the user cin>>unit; //store the entered value in variable unit if(unit>0 && unit<=100){//when this statement is true cout<<"Bill amount is: "; cout<<unit*5;//this statement is Executed otherwise } else if(unit>100 && unit<=200){//when this statement is true cout<<"Bill amount is: "; cout<<(100*5)+(unit-100)*7;//this statement is Executed otherwise } else if( unit>200 && unit<=300){//when this statement is true cout<<"Bill amount is: "; cout<<(100*5)+(100*7)+(unit-200)*10;//this statement is Executed otherwise } else if(unit>300){//when this statement is true cout<<"Bill amount is: "; cout<<(100*5)+(100*7)+(100*10)+(unit-300)*15;//this statement is Executed otherwise } else{//when all statements are false cout<<"Bill amount is: "; cout<<"No value";//this statement is Executed } getch(); return 0; }
When the above code is executed, it produces the following result
Enter the unit of your usage: 360 Bill amount is: 3100
Calculate Electricity bill using function in C++ language
In this program, we will calculate electricity bill using function in C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; int calc_Electricity(int);//function prototype int main() { int unit=0; cout<<"Enter total number of units consumed: "; cin>>unit; calc_Electricity(unit);//call the function to produced output getch(); return 0; } int calc_Electricity(int unit){//function definition double amount; if(unit<=50)//When the statement is true {//below 50 units amount=unit*1.50;//this is executed, otherwise move to next } else if(unit<=150)//When the statement is true {//below 150 units amount=((50*1.5)+(unit-50)*2.00);//this is executed, otherwise move to next } else if((unit<=250)){//When the statement is true //below 250 units amount=(50*1.5)+((150-50)*2.00)+(unit-150)*3.00;//this is executed, otherwise move to next } else{//above 25 units amount=(50*1.5)+((150-50)*2.00)+((250-150)*3.00)+(unit-250)*4; //this is executed } cout<<"Electricity bill = Rs."<<amount; //display the output on the screen }
When the above code is executed, it produces the following result
Enter total number of units consumed: 380 Electricity bill= Rs.1095
Calculate Electricity bill using object in C++ language
In this program, we will calculate electricity bill using class and object in C++ language
Program 2
#include <iostream> #include <conio.h> using namespace std; class EleBill { private: int cUnit,pUnit,amt,unit; public: void get(); void printAmount(); }; void EleBill::get() { cout << "Enter previous unit:" << endl; cin>>pUnit;//ask input from the user for previous unit cout << "Enter current unit:" << endl; cin>>cUnit;//ask input from the user for current unit } /*1 - 100 = 0.5 101 - 200 = 1 201 - 300 = 3 above 300 - 5*/ void EleBill::printAmount() { unit=cUnit-pUnit; if(unit>0 && unit<=100) { amt=unit*0,5; } if(unit>100 && unit<=200) { amt=unit*1; } if(unit>200 && unit<=300) { amt=unit*3; } if(unit>300) { amt=unit*5; } cout << "Bill charge: " <<amt <<endl; } int main() { EleBill o;//create object for class EleBill o.get();//Call the get() method using object o.printAmount(); //Call the printAmount() method using object getch(); return 0; }
When the above code is executed, it produces the following result
Enter previous unit: 543 Enter current unit: 789 Bill charge:738
Suggested post
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