# Program to calculate Electricity Bill

## Program to calculate Electricity Bill

Given an integer U denoting the amount of KWh units of electricity consumed, the task is to calculate the electricity bill with the help of the below charges:

• 1 to 100 units –
• 100 to 200 units –
• 200 to 300 units –
• above 300 units –

Examples:

Output: 3500
Explanation:
Charge for the first 100 units – 10*100 = 1000
Charge for the 100 to 200 units – 15*100 = 1500
Charge for the 200 to 250 units – 20*50 = 1000
Total Electricity Bill = 1000 + 1500 + 1000 = 3500
Input: U = 95
Output: 950
Explanation:
Charge for the first 100 units – 10*95 = 950
Total Electricity Bill = 950

Approach: The idea is to identify the charge bar in which it falls and then calculate the bill according to the charges mentioned above. Below is the illustration of the steps:

• Check units consumed is less than equal to the 100, If yes then the total electricity bill will be:

• Else if, check that units consumed is less than equal to the 200, if yes then total electricity bill will be:

• Else if, check that units consumed is less than equal to the 300, if yes then total electricity bill will be:

• Else if, check that units consumed greater than 300, if yes then total electricity bill will be:

Below is the implementation of the above approach:

 // C++ implementation to calculate the // electricity bill #include using namespace std; // Function to calculate the // electricity bill int calculateBill(int units) {     // Condition to find the charges     // bar in which the units consumed     // is fall     if (units <= 100)     {         return units * 10;     }     else if (units <= 200)     {         return (100 * 10) +                (units - 100) * 15;     }     else if (units <= 300)     {         return (100 * 10) +                (100 * 15) +                (units - 200) * 20;     }     else if (units > 300)     {         return (100 * 10) +                (100 * 15) +                (100 * 20) +                (units - 300) * 25;     }     return 0; } // Driver Code int main() {     int units = 250;     cout << calculateBill(units); } // This code is contributed by spp____

Output:

3500

Last Updated on November 13, 2021 by admin

## How to assign values to variables in C++How to assign values to variables in C++

How to assign values to variables in Python and other languages This article discusses methods to assign values to variables.   Method 1: Direct Initialisation Method   // C++ code to demonstrate variable assignment // upon condition using Direct Initialisation

## Program to print all the numbers divisible by 3 and 5 for a given numberProgram to print all the numbers divisible by 3 and 5 for a given number

Program to print all the numbers divisible by 3 and 5 for a given number Given the integer N, the task is to print all the numbers less than N, which are divisible by 3 and 5. Examples : Input

## Program to print the diamond shapeProgram to print the diamond shape

Program to print the diamond shape Given a number n, write a program to print a diamond shape with 2n rows. Examples : // C++ program to print diamond shape // with 2n rows #include <bits/stdc++.h> using namespace std; //

## Count occurrences of a word in stringCount occurrences of a word in string

Count occurrences of a word in string You are given a string and a word your task is that count the number of the occurrence of the given word in the string and print the number of occurrences of the

## Generating subarrays using recursionGenerating subarrays using recursion

Generating subarrays using recursion Given an array, generate all the possible subarrays of the given array using recursion. Examples:   Input : [1, 2, 3] Output : [1], [1, 2],[1,3], [2], [1, 2, 3], [2, 3], [3] Input : [1,

## Is sizeof for a struct equal to the sum of sizeof of each member?Is sizeof for a struct equal to the sum of sizeof of each member?

Is sizeof for a struct equal to the sum of sizeof of each member? The sizeof for a struct is not always equal to the sum of sizeof of each individual member. This is because of the padding added by

## Program to generate all possible valid IP addresses from given stringProgram to generate all possible valid IP addresses from given string

Program to generate all possible valid IP addresses from given string Given a string containing only digits, restore it by returning all possible valid IP address combinations. A valid IP address must be in the form of A.B.C.D, where A,

## Array of Strings in C++Array of Strings in C++

Array of Strings in C++ (All Different Ways to Create) In C and C++, a string is a 1-dimensional array of characters and an array of strings in C is a 2-dimensional array of characters. There are many ways to