Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

โปรแกรม C คำนวณค่าไฟฟ้า (Electricity Bill) แบบขั้นบันได


คำนวณค่าไฟฟ้าตามหน่วยที่ใช้: ใช้มากกว่า จ่ายอัตราสูงกว่า (Progressive Rate)

อัตราค่าไฟฟ้า (ตัวอย่าง)

  • 0-50 หน่วย: 3.50 บาท/หน่วย + ค่าบริการ 25 บาท
  • 51-100 หน่วย: 130 บาท (50 หน่วยแรก) + 4.25 บาท/หน่วยเกิน + ค่าบริการ 35 บาท
  • 101-200 หน่วย: 292.50 บาท (100 หน่วยแรก) + 5.26 บาท/หน่วยเกิน + ค่าบริการ 45 บาท
  • เกิน 200 หน่วย: 818.50 บาท (200 หน่วยแรก) + 7.75 บาท/หน่วยเกิน + ค่าบริการ 55 บาท

โปรแกรมตัวอย่าง

#include <stdio.h>
int main() {
    int units;
    float amount, serviceCharge, total;
    
    printf("Enter units consumed: ");
    scanf("%d", &units);
    
    if (units < 50) {
        amount = units * 3.50;
        serviceCharge = 25;
    } else if (units <= 100) {
        amount = 130 + ((units - 50) * 4.25);
        serviceCharge = 35;
    } else if (units <= 200) {
        amount = 130 + 162.50 + ((units - 100) * 5.26);
        serviceCharge = 45;
    } else {
        amount = 130 + 162.50 + 526 + ((units - 200) * 7.75);
        serviceCharge = 55;
    }
    
    total = amount + serviceCharge;
    printf("Electricity Bill = %.2f\n", total);
    return 0;
}

ผลลัพธ์

Enter no of units consumed: 280
electricity bill = 1493.50