0% found this document useful (0 votes)
7 views1 page

Cal

This C++ program implements a simple calculator that performs basic arithmetic operations: addition, subtraction, multiplication, and division. It prompts the user to enter an operator and two numbers, then calculates and displays the result based on the chosen operation. The program also handles division by zero and invalid operators with appropriate error messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views1 page

Cal

This C++ program implements a simple calculator that performs basic arithmetic operations: addition, subtraction, multiplication, and division. It prompts the user to enter an operator and two numbers, then calculates and displays the result based on the chosen operation. The program also handles division by zero and invalid operators with appropriate error messages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

int main() {
char op;
double num1, num2;

std::cout << "Enter operator (+, -, *, /): ";


std::cin >> op;

std::cout << "Enter two numbers: ";


std::cin >> num1 >> num2;

switch(op) {
case '+':
std::cout << num1 << " + " << num2 << " = " << num1 + num2 <<
std::endl;
break;
case '-':
std::cout << num1 << " - " << num2 << " = " << num1 - num2 <<
std::endl;
break;
case '*':
std::cout << num1 << " * " << num2 << " = " << num1 * num2 <<
std::endl;
break;
case '/':
if (num2 != 0)
std::cout << num1 << " / " << num2 << " = " << num1 / num2 <<
std::endl;
else
std::cout << "Error: Division by zero is not allowed." <<
std::endl;
break;
default:
std::cout << "Invalid operator!" << std::endl;
break;
}

return 0;
}

You might also like