-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuoi4.c
More file actions
58 lines (48 loc) · 1.67 KB
/
buoi4.c
File metadata and controls
58 lines (48 loc) · 1.67 KB
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
#include <stdio.h>
int main() {
float num1, num2;
int choice;
// Prompt for and read num1
printf("Nhập số thứ nhất (num1): ");
scanf("%f", &num1);
// Flush the input buffer to consume the newline character
while (getchar() != '\n');
// Prompt for and read num2
printf("Nhập số thứ hai (num2): ");
scanf("%f", &num2);
// Flush the input buffer to consume the newline character
while (getchar() != '\n');
// Display the menu for the user
printf("Chọn phép tính:\n");
printf("1. Cộng\n");
printf("2. Trừ\n");
printf("3. Nhân\n");
printf("4. Chia\n");
printf("Nhập lựa chọn của bạn (1-4): ");
// Read the user's choice
choice = getchar() - '0'; // Convert the character to an integer
// Flush the input buffer to consume the newline character
while (getchar() != '\n');
// Perform the selected operation and display the result
switch (choice) {
case 1:
printf("Kết quả: %.2f + %.2f = %.2f\n", num1, num2, num1 + num2);
break;
case 2:
printf("Kết quả: %.2f - %.2f = %.2f\n", num1, num2, num1 - num2);
break;
case 3:
printf("Kết quả: %.2f * %.2f = %.2f\n", num1, num2, num1 * num2);
break;
case 4:
if (num2 != 0) {
printf("Kết quả: %.2f / %.2f = %.2f\n", num1, num2, num1 / num2);
} else {
printf("Lỗi: Không thể chia cho 0.\n");
}
break;
default:
printf("Lựa chọn không hợp lệ.\n");
}
return 0;
}