Back Button
Back to Chapter
C++ Introduction
No items found.
C++ Basics
No items found.
C++ Control Statements
No items found.
C++ Logical Operators
No items found.
C++ Procedural Programming
No items found.
C++ Structural Programming
No items found.
C++ Implementation of OOPS
No items found.
C++ Arrays and Vectors
No items found.
C++ Error Handling
No items found.
C++ File Handling
No items found.
Collapse_Icon
Table of Contents

Make a simple calculator that accepts 2 operands from the user and performs the operation, based on the knowledge you gained in the previous chapters.

Solution

# include using namespace std; int main() { char op; float num1, num2; cout << "Enter operand 1: "; cin >> num1; cout<<"enter operand 2:"; cin>>num2 cout << "Enter operator either + or - or * or /: "; cin >> op; switch(op) { case '+': cout << num1+num2; break; case '-': cout << num1-num2; break; case '*': cout << num1*num2; break; case '/': if(num1>num2) cout << num1/num2; else cout<
Output: Enter operand 1: 10 Enter operand 2: 60 6