C Program to Calculate Electricity Bill

Introduction

This program's objective is to compute the power bill for a specified number of consumed units. The program is crucial because it makes it simple and quick to determine the price of electricity, which is useful for planning and budgeting.

The way the program operates is by first asking the user how many units they've consumed. The program then determines the cost per unit according to the quantity consumed. The program then totals the electrical costs and prints them to the console.

The program's primary elements are as follows:

The main function: This serves as the program's entrance. It is in charge of gathering user input, invoking the function that computes the power bill, and showing the user the results.

The function calculate_electricity_bill: The electricity bill calculation is done by this function. The entire cost of the power bill is returned after the previous metre reading, the present meter reading, and the tariff rates have been entered.

The minimum operation: The lowest value of two numbers can be found with this function. It is employed to calculate how many units are utilized in each layer.

The procedures for computing an electricity bill:

The procedures for computing an electrical bill with a C programme are as follows:

1. Bring in the required libraries.
2. Set the parameters.
3. Obtain the user's input.
4. Determine the price per unit.
5. Determine the whole expense.
6. Publish the results.

User Input:

1. The user will be prompted by the programme to enter the tariff rates first. The various costs that the utility company charges per unit of energy are known as tariff rates. The cost per unit of power for each tier can be entered by the user as a list of numbers in the tariff rates.
2. The cost per unit of power, for instance, might be 2.5 for the first 50 units consumed, 3.5 for the following 50 units, and 5.0 for all units spent above 100.
3. Following that, the program will ask the user to enter the previous meter reading. The amount of electricity used during the previous billing cycle is represented by the preceding meter reading. The previous meter reading can be entered by the user as an integer.
4. The user will then be prompted by the programme to enter the most recent metre reading. The amount of electricity used since the last billing cycle is indicated by the current metre reading. The current metre reading can be entered by the user as an integer.
5. The program may compute the electricity bill once the user has input all the required data. The total number of units used will be multiplied by the cost per unit, accounting for tariff rates, to get the amount of the power bill.

The software will compute the electricity bill and print it to the console once the user has input all the required data.

Invalid input should also be checked by the program. For instance, the program should produce an error message and prompt the user to provide the information again if the user inputs a negative number for either the previous or current meter reading.

The procedures used to determine the user's input-based electricity bill calculation:

1. Determine the quantity of units consumed. To do this, subtract the most recent metre reading from the previous one.
2. Repeat the tariff rates iteratively. Determine the price for the quantity consumed at each tariff rate for that tier.

Add the prices for every tier. The whole cost of the electricity bill is indicated here.

```float calculate_electricity_bill(float tariff_rates[], int prior_meter_reading, int current_meter_reading) {

float total_cost = 0.0;

for (int i = 0; i < sizeof(tariff_rates) / sizeof(float); i++) {

float cost_per_unit = tariff_rates[i];

float cost = cost_per_unit * units_consumed;

total_cost += cost;

}

}```

The total cost is initially set to 0 by this algorithm. The tariff rates are then repeated. It determines the price per unit and the quantity consumed under each tariff rate. Next, the cost per unit is multiplied by the quantity of units consumed to determine the cost for the tier.

The cost for the tier is then added, updating the total cost. The algorithm then delivers the overall cost.

Here is an illustration of how the algorithm might operate:

```tariff_rates = [2.5, 3.5, 5.0]

print(total_cost)```

The whole cost of the electricity bill, which would be \$43.50, would be printed by this code. This is due to the fact that the first 50 units are charged at a rate of 2.5, the following 50 units at a rate of 3.5, and any units over 100 are charged at a rate of 5.0.

The program's variables are listed below along with what they are used for:

tariff_rates: This is a table of numbers that shows how much each tier will cost you to buy one unit of electricity.

prior_meter_reading: This is the total amount of electricity used during the last billing period.

current_meter_reading: The quantity of electricity used since the last billing cycle is represented by this number.

units_consumed: The total amount of electricity that has been consumed is shown here. It is computed by deducting the most recent metre reading from the previous one.

total_cost: The total cost of the electricity bill is indicated here. The cost per unit is multiplied by the quantity of units consumed to arrive at the final number.

• To guarantee the data's accuracy. Unvalidated user input may contain errors, which could produce unreliable results. For instance, the programme would calculate the electricity bill inaccurately if the user enters a negative figure for the previous metre reading.
• To stop malicious assaults. A program's vulnerabilities may be exploited using user input, which could result in theft of data or unauthorised access. The programme might be misled into executing malicious code, for instance, if a user types in a string of code that is intended to do harm.
• In order to enhance user experience. Incorrect processing of user input by the programme could result in mistakes or unexpected behaviour if it is not validated. Users may find it frustrating and challenging to utilise the programme as a result of this.

Here are a few illustrations of input validation methods:

• A search for numerical quantities. Use the is_numeric() method in JavaScript or the isdigit() function in Python to accomplish this.
• Confirming that the metre values fall within an acceptable range. Checking if the numbers are higher than or equal to 0 and less than or equal to the maximum value will enable you to achieve this.
• Verifying the input's format. This can be achieved by determining if the input corresponds to a particular pattern, such as a date or phone number.

C program to calculate electricity bill:

```#include <stdio.h>

#include <stdlib.h>

int min(int a, int b) {

return a < b ? a : b;

}

int main() {

float tariff_rates[3];

int units_consumed;

float total_cost = 0.0;

// Get the tariff rates from the user.

for (int i = 0; i < sizeof(tariff_rates) / sizeof(float); i++) {

printf("Enter the tariff rate for tier %d: ", i + 1);

scanf("%f", &tariff_rates[i]);

}

// Get the prior meter reading from the user.

printf("Enter the prior meter reading: ");

// Get the current meter reading from the user.

printf("Enter the current meter reading: ");

// Calculate the number of units consumed.

// Calculate the total cost of the electricity bill.

for (int i = 0; i < sizeof(tariff_rates) / sizeof(float); i++) {

float cost_per_unit = tariff_rates[i];

int units_consumed_in_tier = min(units_consumed, (int)cost_per_unit);

float cost = cost_per_unit * units_consumed_in_tier;

total_cost += cost;

units_consumed -= units_consumed_in_tier;

}

// Print the total cost of the electricity bill in rupees.

printf("The total cost of the electricity bill is: ₹%.2f
", total_cost);

return 0;

}```

Output:

The following major topics were addressed in the tutorial:

To accommodate unique requirements, the programme might be altered.

How to figure out how many units were consumed using the previous and current metre readings.

How to figure out how much your power will cost overall depending on the tariff rates and the amount of units you use.

How to present the user with the generated bill amount in an understandable format.

The program's use in calculating electricity bills has the following advantages:

The software is simple to use and comprehend.

The process of running the programme is quick and effective.