Courses

# Calculator Program

0 Suresh Chand March 31, 2021

In this example, We will learn to make a simple calculator using c++ programming language. This calculator makes mathematics operations like addition, multiplication, and division. We will use a switch case for this example.

Let’s look an code

## Calculator Program CODE:

In this program, First we will ask for two numbers and one operator. If user provide valid operator then using switch case we will perform operation.

``````#include <iostream>

using namespace std;

// Main program
int main()

{

char oper;

float a, b;

//We will ask to user for operands
cout<<"Enter two operands: ";

cin>> a >> b;

//We will ask to user to enter operator i.e. +, -, *, /
cout<<"Enter operator: ";

cin>> oper;

switch (oper)

{

// If operator is '+'
case '+':

cout << a + b;

break;

// If operator is '-'
case '-':

cout << a - b;

break;

// If operator is '*'

case '*':

cout << a * b;

break;

// If operator is '/'

case '/':

cout << a / b;

break;

// If any other operator display error message

default:

cout << "Please enter valid operator!!!";

break;

}

return 0;

}``````

The output of above program is

``````Enter two operands: 12
13
Enter operator: +
25

Enter two operands: 12
13
Enter operator: &
Subscribe
Notify of
Inline Feedbacks