-
Notifications
You must be signed in to change notification settings - Fork 0
/
ElectricityConsumption.c
85 lines (69 loc) · 2.17 KB
/
ElectricityConsumption.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <stdio.h>
#define FREE_UNITS 100
#define RATE1_UNITS 300
#define RATE2_UNITS 400
#define RATE3_UNITS 500
#define RATE1_PER_UNIT 2.25
#define RATE2_PER_UNIT 4.50
#define RATE3_PER_UNIT 6.00
#define RATE4_PER_UNIT 8.00
#define TOTAL_HOUSES 5
struct Consumer {
char name[50];
int ebNumber;
int unitsConsumed;
float billAmount;
};
void calculateBill(struct Consumer *consumer) {
int units = consumer->unitsConsumed;
float bill = 0.0;
if (units <= FREE_UNITS) {
consumer->billAmount = 0.0;
return;
} else {
units -= FREE_UNITS;
}
if (units <= RATE1_UNITS) {
bill += units * RATE1_PER_UNIT;
} else {
bill += RATE1_UNITS * RATE1_PER_UNIT;
units -= RATE1_UNITS;
if (units <= RATE1_UNITS) {
bill += units * RATE2_PER_UNIT;
} else {
bill += RATE2_UNITS * RATE2_PER_UNIT;
units -= RATE2_UNITS;
if (units <= RATE1_UNITS) {
bill += units * RATE3_PER_UNIT;
} else {
bill += RATE3_UNITS * RATE3_PER_UNIT;
units -= RATE3_UNITS;
bill += units * RATE4_PER_UNIT;
}
}
}
consumer->billAmount = bill;
}
int main() {
struct Consumer consumers[TOTAL_HOUSES];
printf("Enter the details for %d houses:\n", TOTAL_HOUSES);
for (int i = 0; i < TOTAL_HOUSES; i++) {
printf("\nHouse %d:\n", i + 1);
printf("Name: ");
scanf("%s", consumers[i].name);
printf("EB Number: ");
scanf("%d", &consumers[i].ebNumber);
printf("Units Consumed: ");
scanf("%d", &consumers[i].unitsConsumed);
calculateBill(&consumers[i]);
}
printf("\nElectricity Bills:\n");
for (int i = 0; i < TOTAL_HOUSES; i++) {
printf("\nHouse %d:\n", i + 1);
printf("Name: %s\n", consumers[i].name);
printf("EB Number: %d\n", consumers[i].ebNumber);
printf("Units Consumed: %d\n", consumers[i].unitsConsumed);
printf("Bill Amount: %.2f INR\n", consumers[i].billAmount);
}
return 0;
}