Average Monthly Electricity Bill¶
Concept Explanation¶
What is it?¶
This program calculates the average (mean) of three given electricity bills. The average is a measure of central tendency, representing a typical value for a set of numbers. It's found by summing all values and dividing by the count of those values.
Why is it important?¶
Calculating averages is a very common task in data analysis, statistics, and everyday life. In programming, it serves as a straightforward example of applying arithmetic operations to a small dataset to derive a meaningful summary.
Where is it used?¶
- Personal Finance: Tracking average monthly expenses or income.
- Business Analytics: Calculating average sales, customer spending, or production costs.
- Science and Research: Determining average experimental readings or environmental data.
- Performance Metrics: Averaging performance scores, response times, etc.
Real-world example¶
If your electricity bills for the last three months were $23, $32, and $64, calculating the average helps you understand your typical monthly expenditure. The sum is $23 + $32 + $64 = $119. The average is $119 / 3 = $39.67. This average gives you a single figure to budget around.
Algorithm¶
- Start.
- Define or get three electricity bills (
bill1,bill2,bill3). - Calculate the total sum of the bills:
total_bill = bill1 + bill2 + bill3. - Calculate the average monthly bill:
average_bill = total_bill / 3. - Display the individual bills, total bill, and the calculated average monthly bill.
- End.
Edge Cases: - Non-numeric input for bills (handled by language-specific error mechanisms or explicit validation). - Zero or negative bill amounts (program will handle them mathematically, but negative bills are not realistic in this context and might require validation).
Implementations¶
public class AverageElectricityBill {
public static void main(String[] args) {
// Electricity bills for the last three months (can be user input in a modified version)
double bill1 = 23.0;
double bill2 = 32.0;
double bill3 = 64.0;
// Calculate the sum of the bills
double totalBill = bill1 + bill2 + bill3;
// Calculate the average monthly bill
double averageBill = totalBill / 3;
System.out.println("Electricity Bill - Month 1: $" + bill1);
System.out.println("Electricity Bill - Month 2: $" + bill2);
System.out.println("Electricity Bill - Month 3: $" + bill3);
System.out.println("Total Bill for three months: $" + String.format("%.2f", totalBill)); // Formatted for clarity
System.out.println("Average Monthly Electricity Bill: $" + String.format("%.2f", averageBill));
}
}
# Electricity bills for the last three months
bill1 = 23.0
bill2 = 32.0
bill3 = 64.0
# Calculate the sum of the bills
total_bill = bill1 + bill2 + bill3
# Calculate the average monthly bill
average_bill = total_bill / 3
print(f"Electricity Bill - Month 1: ${bill1}")
print(f"Electricity Bill - Month 2: ${bill2}")
print(f"Electricity Bill - Month 3: ${bill3}")
print(f"Total Bill for three months: ${total_bill:.2f}")
print(f"Average Monthly Electricity Bill: ${average_bill:.2f}")
#include <stdio.h>
int main() {
double bill1 = 23.0; // Electricity bill for month 1
double bill2 = 32.0; // Electricity bill for month 2
double bill3 = 64.0; // Electricity bill for month 3
double average_bill;
// Calculate the average monthly electricity bill
average_bill = (bill1 + bill2 + bill3) / 3.0;
printf("Electricity Bill - Month 1: $%.2lf", bill1);
printf("Electricity Bill - Month 2: $%.2lf", bill2);
printf("Electricity Bill - Month 3: $%.2lf", bill3);
printf("Total Bill for three months: $%.2lf", bill1 + bill2 + bill3); // Display total as well
printf("Average Monthly Electricity Bill: $%.2lf", average_bill);
return 0;
}
SET SERVEROUTPUT ON;
DECLARE
bill1 NUMBER := 23;
bill2 NUMBER := 32;
bill3 NUMBER := 64;
total_bill NUMBER;
average_bill NUMBER;
BEGIN
DBMS_OUTPUT.PUT_LINE('--- Average Monthly Electricity Bill ---');
DBMS_OUTPUT.PUT_LINE('Electricity Bill - Month 1: $' || bill1);
DBMS_OUTPUT.PUT_LINE('Electricity Bill - Month 2: $' || bill2);
DBMS_OUTPUT.PUT_LINE('Electricity Bill - Month 3: $' || bill3);
total_bill := bill1 + bill2 + bill3;
average_bill := total_bill / 3;
DBMS_OUTPUT.PUT_LINE('Total Bill for three months: $' || ROUND(total_bill, 2));
DBMS_OUTPUT.PUT_LINE('Average Monthly Electricity Bill: $' || ROUND(average_bill, 2));
END;
/
Explanation¶
- Java: Uses
doubleto handle monetary values. The bills are hardcoded but can be easily modified to take user input.String.format("%.2f", ...)ensures currency formatting. - Python: Bills are hardcoded as floats. Uses standard arithmetic operators.
f-stringsformat the output to two decimal places. - C: Uses
doublefor bill amounts. Hardcoded values are used.printf("$%.2lf", ...)formats the output for currency. - Oracle: Implemented in PL/SQL. Uses
NUMBERdata type. Bills are hardcoded constants.ROUND()is used for formatting the output to two decimal places.
Complexity Analysis¶
- Time Complexity: O(1) - The number of arithmetic operations is constant.
- Space Complexity: O(1) - A fixed number of variables are used.
Flowchart¶
graph TD
A[Start] --> B[Define bill1, bill2, bill3]
B --> C[Calculate Total Bill = bill1 + bill2 + bill3]
C --> D[Calculate Average Bill = Total Bill / 3]
D --> E[Display Bills, Total, and Average]
E --> F[End]
Sample Dry Run¶
| Step | bill1 | bill2 | bill3 | Total Bill | Average Bill | Description |
|---|---|---|---|---|---|---|
| Initial | 23.0 | 32.0 | 64.0 | - | - | Bills are defined |
| Process | 23.0 | 32.0 | 64.0 | 119.0 | - | Total Bill = 23.0 + 32.0 + 64.0 = 119.0 |
| Process | 23.0 | 32.0 | 64.0 | 119.0 | 39.67 | Average Bill = 119.0 / 3 = 39.666... (rounded to 39.67) |
| Output | - | - | - | - | - | Display bills, total, and average |
| End | - | - | - | - | - | Program terminates |
Practice Problems¶
Easy¶
- Modify the program to take the three bills as user input.
- Calculate the average of N bills, where N is also provided by the user.
Medium¶
- Implement a system to track bills over a year and calculate monthly, quarterly, and annual averages.
- Add input validation to ensure bill amounts are positive.
Hard¶
- Create a budgeting tool that uses historical average bill data to predict future expenses and alert users if their current spending is above average.
"The only source of knowledge is experience." - Albert Einstein