Advertisement
Spocoman

02. Operations

Oct 26th, 2023 (edited)
628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. void addition(int a, int b) {
  6.     cout << a + b << endl;
  7. }
  8. void subtraction(int a, int b) {
  9.     cout << a - b << endl;
  10. }
  11. void multiplication(int a, int b) {
  12.     cout << a * b << endl;
  13. }
  14. void division(int a, int b) {
  15.     if (b != 0) {
  16.         cout << (double)a / b << endl;
  17.     }
  18.     else {
  19.         cout << "Can't divide by zero." << endl;
  20.     }
  21. }
  22.  
  23. int main() {
  24.     double n1, n2;
  25.     cin >> n1 >> n2;
  26.  
  27.     char operation;
  28.     cin >> operation;
  29.  
  30.     if (operation == '+') {
  31.         addition(n1, n2);
  32.     }
  33.     else if (operation == '-') {
  34.         subtraction(n1, n2);
  35.     }
  36.     else if(operation == '*') {
  37.         multiplication(n1, n2);
  38.     }
  39.     else {
  40.         division(n1, n2);
  41.     }
  42.  
  43.     return 0;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement