0% found this document useful (0 votes)
8 views2 pages

Question 4

The document contains code for a C++ program that takes in two integers and an operator from the user and performs the mathematical operation on the integers. It uses conditionals and a switch statement to handle the different operators and prevent division by zero.

Uploaded by

bwalyanelson99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Question 4

The document contains code for a C++ program that takes in two integers and an operator from the user and performs the mathematical operation on the integers. It uses conditionals and a switch statement to handle the different operators and prevent division by zero.

Uploaded by

bwalyanelson99
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

#include <iostream>

#include <iomanip>

Using namespace std;

Int main()

Int num1, num2;

Char opr;

Cout ≪ “Enter two integers: “;

Cin ≫ num1 ≫ num2;

Cout ≪ endl;

Cout ≪ “Enter operator: + (addition), - (subtraction),” ≪ “ * (multiplication), / (division): “;

Cin ≫ opr;

Cout ≪ endl;

Cout ≪ num1 ≪ “ “ ≪ opr ≪ “ “ ≪ num2 ≪ “ = “;

Switch (opr){

Case ‘+’:

Cout ≪ num1 + num2 ≪ endl;

Break;

Case’-‘:

Cout ≪ num1 – num2 ≪ endl;

Break;

Case’*’:

Cout ≪ num1 * num2 ≪ endl;

Break;

Case’/’:

If (num2 != 0)
Cout ≪ num1 / num2 ≪ endl;

Else

Cout ≪ “ERROR \nCannot divide by zero” ≪ endl;

Break;

Default:

Cout ≪ “Illegal operation” ≪ endl;

Return 0;

You might also like