-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask2.cpp
More file actions
73 lines (62 loc) · 1.95 KB
/
Copy pathTask2.cpp
File metadata and controls
73 lines (62 loc) · 1.95 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
int main() {
int operation;
double num1, num2, result;
// Display a menu of operations
std::cout << "Welcome to the Basic Calculator!" << std::endl;
std::cout << "Available operations:" << std::endl;
std::cout << "1. Addition (+)" << std::endl;
std::cout << "2. Subtraction (-)" << std::endl;
std::cout << "3. Multiplication (*)" << std::endl;
std::cout << "4. Division (/)" << std::endl;
// Get the user's choice of operation
std::cout << "Enter the number corresponding to the operation you want to perform: ";
std::cin >> operation;
if (operation < 1 || operation > 4) {
std::cout << "Invalid operation selected." << std::endl;
return 1; // Exit the program with an error code
}
// Get the two numbers from the user
std::cout << "Enter the first number: ";
std::cin >> num1;
std::cout << "Enter the second number: ";
std::cin >> num2;
// Perform the selected operation
switch (operation) {
case 1:
result = num1 + num2;
break;
case 2:
result = num1 - num2;
break;
case 3:
result = num1 * num2;
break;
case 4:
if (num2 == 0) {
std::cout << "Error! Division by zero is not allowed." << std::endl;
return 1; // Exit the program with an error code
}
result = num1 / num2;
break;
}
// Display the result
std::cout << "Result: " << num1 << " ";
// Display the operator based on the user's choice
switch (operation) {
case 1:
std::cout << "+";
break;
case 2:
std::cout << "-";
break;
case 3:
std::cout << "*";
break;
case 4:
std::cout << "/";
break;
}
std::cout << " " << num2 << " = " << result << std::endl;
return 0;
}