Electricity Bill – if-else-if – successive slab-wise rates – CSKC
Sign In

Search

Code Learning #CPP#1929    siteicon   siteicon  

Electricity Bill – if-else-if – successive slab-wise rates

Classic case of successive slab wise rates calculation for electricity bill generation solved using if-else-if and slab rate formulas.

Learning Objectives

  • Using if-else-if ladder to calculate successive slab rate based on formulas used in electricity bill generation calculation.

Source Code

TC++ #1929

 

Source Code

×

Ask AI About This Code

Default Prompt that is passed along with the code is given below, You can write your own prompt if you so desire.



×

Test CPP

Code Understanding

int units; float rate1=4.5,rate2=5.5,rate3=6.5,rate4=7.0; float bill;
Required variables are declared and initialised here. Bill rate slabs are also given as initialised values.

cout<<“Enter unit consumed : “;cin>>units;
No. of units consumption is collected from the user.

if(units<=200) bill=units*rate1; 
This is lowest slab where unit are just multiplied with rate1.

else if(units>200 && units<=400) bill=(units-200)*rate2+200*rate1;
In this second slab the first slab rate portion is as per lowest rate and above that consumption (units-200) is charged at the higher rate of rate2.

else if(units>400 && units<=600) bill=(units-400)*rate3+200*rate2+200*rate1;
In this case two lower slabs are charged at their respective rate and the balance (units-400) is charged at rate3.

else bill=(units-600)*rate4+200*rate3+200*rate2+200*rate1;
This is highest slab where again three lower slabs are charged at respective rates and balance (units-600) is charged at highest rate of rate4.

cout<<“Your Bill Amount = “<<bill<<endl;
This is printing of finally calculated bill.

 

Notes

  • The currency can be printed after the bill amount as per the country of operations.
  • Since division calculation are not involved in this problem. Both units and rates can be taken as integer values also. Some electricity companies do allow such whole number calculations for billing.
  • Students are advised to try this code by filling real life data of their home with correctly marked slabs. This will provide more confidence in programming when you are able to verify your electricity bill using this kind of program.

Common Errors

  • Mutually exclusive slabs are important. If not taken correctly, wrong charging will appear.


Suggested Filename(s): elect-bill.cpp, bill-slabwise.cpp



Share

Created: 27-Dec-2017 02:53 | Updated: 15-Sep-2018 11:49|