fork download
  1. #include <stdio.h>
  2.  
  3. void calculateElectricityBill() {
  4. float units, totalBill;
  5.  
  6. printf("\nกรุณากรอกจำนวนหน่วยไฟฟ้าที่ใช้ (kWh): ");
  7. scanf("%f", &units);
  8.  
  9. // ตัวอย่างการคำนวณค่าไฟฟ้า (อัตราค่าบริการสมมติ)
  10. if (units <= 50) {
  11. totalBill = units * 3.50; // อัตราค่าไฟ 3.50 บาทต่อหน่วย
  12. } else if (units <= 150) {
  13. totalBill = (50 * 3.50) + ((units - 50) * 4.00);
  14. } else {
  15. totalBill = (50 * 3.50) + (100 * 4.00) + ((units - 150) * 4.50);
  16. }
  17.  
  18. printf("ค่าไฟฟ้าของคุณคือ: %.2f บาท\n", totalBill);
  19. }
  20.  
  21. int main() {
  22. int choice;
  23.  
  24. do {
  25. // แสดงเมนู
  26. printf("\n==== โปรแกรมคำนวณค่าไฟฟ้า ====\n");
  27. printf("1. คำนวณค่าไฟฟ้า\n");
  28. printf("2. ออกจากโปรแกรม\n");
  29. printf("เลือกเมนู: ");
  30. scanf("%d", &choice);
  31.  
  32. // ตรวจสอบตัวเลือกของผู้ใช้
  33. switch (choice) {
  34. case 1:
  35. calculateElectricityBill();
  36. break;
  37. case 2:
  38. printf("ออกจากโปรแกรม...\n");
  39. break;
  40. default:
  41. printf("กรุณาเลือกเมนูที่ถูกต้อง!\n");
  42. }
  43.  
  44. } while (choice != 2); // ทำซ้ำจนกว่าผู้ใช้เลือกเมนู 2
  45.  
  46. return 0;
  47. }
  48.  
Success #stdin #stdout 0.02s 25776KB
stdin
Standard input is empty
stdout
#include <stdio.h>

void calculateElectricityBill() {
    float units, totalBill;

    printf("\nกรุณากรอกจำนวนหน่วยไฟฟ้าที่ใช้ (kWh): ");
    scanf("%f", &units);

    // ตัวอย่างการคำนวณค่าไฟฟ้า (อัตราค่าบริการสมมติ)
    if (units <= 50) {
        totalBill = units * 3.50;  // อัตราค่าไฟ 3.50 บาทต่อหน่วย
    } else if (units <= 150) {
        totalBill = (50 * 3.50) + ((units - 50) * 4.00);
    } else {
        totalBill = (50 * 3.50) + (100 * 4.00) + ((units - 150) * 4.50);
    }

    printf("ค่าไฟฟ้าของคุณคือ: %.2f บาท\n", totalBill);
}

int main() {
    int choice;

    do {
        // แสดงเมนู
        printf("\n==== โปรแกรมคำนวณค่าไฟฟ้า ====\n");
        printf("1. คำนวณค่าไฟฟ้า\n");
        printf("2. ออกจากโปรแกรม\n");
        printf("เลือกเมนู: ");
        scanf("%d", &choice);

        // ตรวจสอบตัวเลือกของผู้ใช้
        switch (choice) {
            case 1:
                calculateElectricityBill();
                break;
            case 2:
                printf("ออกจากโปรแกรม...\n");
                break;
            default:
                printf("กรุณาเลือกเมนูที่ถูกต้อง!\n");
        }

    } while (choice != 2); // ทำซ้ำจนกว่าผู้ใช้เลือกเมนู 2

    return 0;
}