Write a program that lets the user perform arithmetic operations on two numbers. Your program must be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numbers. Furthermore, your program must consist of following functions:
1. Function showChoice: This function shows the options to the user and explains how to enter data.
2. Function add: This function accepts two number as arguments and returns sum.
3. Function subtract: This function accepts two number as arguments and returns their difference.
4. Function multiply: This function accepts two number as arguments and returns product.
5. Function divide: This function accepts two number as arguments and returns quotient.

Source Code:

 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
74
75
76
77
78
79
#include <iostream>
using namespace std;

void showChoices();
float add(float, float);
float subtract(float, float);
float multiply(float, float);
float divide(float, float);

int main()
{
 float x, y;
 int choice;
 do
 {
  showChoices();
  cin >> choice;
  switch (choice)
  {
  case 1:
   cout << "Enter two numbers: ";
   cin >> x >> y;
   cout << "Sum " << add(x,y) <<endl;
   break;
  case 2:
   cout << "Enter two numbers: ";
   cin >> x >> y;
   cout << "Difference " << subtract(x,y) <<endl;
   break;
  case 3:
   cout << "Enter two numbers: ";
   cin >> x >> y;
   cout << "Product " << multiply(x,y) <<endl;
   break;
  case 4:
   cout << "Enter two numbers: ";
   cin >> x >> y;
   cout << "Quotient " << divide(x,y) <<endl;
   break;
  case 5:
   break;
  default:
   cout << "Invalid input" << endl;
  }
 }while (choice != 5);

 return 0;
}

void showChoices()
{
 cout << "MENU" << endl;
 cout << "1: Add " << endl;
 cout << "2: Subtract" << endl;
 cout << "3: Multiply " << endl;
 cout << "4: Divide " << endl;
 cout << "5: Exit " << endl;
 cout << "Enter your choice :";
}

float add(float a, float b)
{
 return a + b;
}

float subtract(float a, float b)
{
 return a - b;
}

float multiply(float a, float b)
{
 return a * b;
}

float divide(float a, float b)
{
 return a / b;
}

Post a Comment

 
Top