In this post, we will make a menu driven program in c++ for a simple calculator which will add, subtract, multiply, divide the two numbers
Write a Menu Driven C++ Program for a Simple Calculator
In this program, first we will take two numbers from the user. Then we will display the list of arithmetic operations our program can perform. You also add other arithmetic operations as well like square and root of a number. According to user choice, we will perform the arithmetic operations between them and print the result.
#include <iostream>
using namespace std;
int main()
{
char fav;
int num1, num2, choice, i;
cout << "\n Enter 1st Number:";
cin >> num1;
cout << "\n Enter 2nd Number:";
cin >> num2;
do
{
cout << "\n Press 1 for Addition";
cout << "\n Press 2 for Subtraction";
cout << "\n Press 3 for Multiplication:";
cout << "\n Press 4 for Division";
cout << "\n Enter your choice:";
cin >> choice;
switch (choice)
{
case 1:
cout << "\n Sum of " << num1 << " and " << num2;
cout << " : " << num1 + num2;
break;
case 2:
cout << "\n Subtraction of " << num1 << " and " << num2;
cout << " : " << num1 - num2;
break;
case 3:
cout << "\n Multiplication of " << num1 << " and " << num2;
cout << " : " << num1 * num2;
break;
case 4:
cout << "\n Division of " << num1 << " and " << num2;
cout << " : " << num1 / num2;
break;
default:
printf("\n wrong choice");
}
printf("\n Do you want to continue?(y/n): ");
scanf("\n %c", &fav);
} while (fav == 'y');
}
Output for a Simple Calculator:

Using functions
In this program, we will declare functions for the respective arithmetic operations.
#include <iostream>
using namespace std;
void addition(int a, int b);
void subtraction(int a, int b);
void multiplication(int a, int b);
void division(int a, int b);
int main()
{
char fav;
int num1, num2, choice, i;
cout << "\n Enter 1st Number:";
cin >> num1;
cout << "\n Enter 2nd Number:";
cin >> num2;
do
{
cout << "\n 1. Addition";
cout << "\n 2. Subtraction";
cout << "\n 3. Multiplication:";
cout << "\n 4. Division";
cout << "\n Enter your choice:";
cin >> choice;
switch (choice)
{
case 1:
addition(num1, num2);
break;
case 2:
subtraction(num1, num2);
break;
case 3:
multiplication(num1, num2);
break;
case 4:
division(num1, num2);
break;
default:
printf("\n wrong choice");
}
printf("\n Do you want to continue?(y/n): ");
scanf("\n %c", &fav);
} while (fav == 'y');
}
void addition(int a, int b)
{
cout << a << "+" << b << "=" << a + b;
}
void subtraction(int a, int b)
{
cout << a << "-" << b << "=" << a - b;
}
void multiplication(int a, int b)
{
cout << a << "*" << b << "=" << a * b;
}
void division(int a, int b)
{
if (b == 0)
cout << "\n Denominator can't be zero";
else
cout << a << "/" << b << "=" << a / b;
}
Great Post