fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main() {
  5. int choice;
  6. float kg, price = 0, total = 0;
  7. char product[20];
  8.  
  9. // แสดงรายการสินค้า
  10. printf("===== Product List =====\n");
  11. printf("1. Orange - 45 Baht/kg\n");
  12. printf("2. Papaya - 50 Baht/kg\n");
  13. printf("3. Watermelon - 27 Baht/kg\n");
  14. printf("4. Apple - 60 Baht/kg\n");
  15. printf("5. Banana - 70 Baht/kg\n");
  16.  
  17. // รับค่าจากผู้ใช้
  18. printf("\nPlease select a product (1-5): ");
  19. scanf("%d", &choice);
  20.  
  21. if (choice == 1) {
  22. strcpy(product, "Orange");
  23. price = 45;
  24. } else if (choice == 2) {
  25. strcpy(product, "Papaya");
  26. price = 50;
  27. } else if (choice == 3) {
  28. strcpy(product, "Watermelon");
  29. price = 27;
  30. } else if (choice == 4) {
  31. strcpy(product, "Apple");
  32. price = 60;
  33. } else if (choice == 5) {
  34. strcpy(product, "Banana");
  35. price = 70;
  36. } else {
  37. printf("Invalid selection!\n");
  38. return 0; // จบโปรแกรมถ้าเลือกผิด
  39. }
  40.  
  41. // รับค่าน้ำหนัก
  42. printf("Enter weight (kg): ");
  43. scanf("%f", &kg);
  44.  
  45. // คำนวณราคารวม
  46. total = price * kg;
  47.  
  48. // แสดงผลลัพธ์
  49. printf("\n===== Receipt =====\n");
  50. printf("Product : %s\n", product);
  51. printf("Weight : %.2f kg\n", kg);
  52. printf("Price/kg : %.2f Baht\n", price);
  53. printf("Total : %.2f Baht\n", total);
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
stdout
===== Product List =====
1. Orange     - 45 Baht/kg
2. Papaya     - 50 Baht/kg
3. Watermelon - 27 Baht/kg
4. Apple      - 60 Baht/kg
5. Banana     - 70 Baht/kg

Please select a product (1-5): Invalid selection!