0% found this document useful (0 votes)
3K views

Program Code

Uploaded by

mp7616340
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)
3K views

Program Code

Uploaded by

mp7616340
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

PROGRAM CODE

#include <iostream>
#include <cmath> // For pow() and sqrt()
using namespace std;

int main()
{
char operation;
double num1, num2;
cout << "Enter an operator (+, -, *, /, %, ^, r for square root): ";
cin >> operation;
if (operation == 'r')
{
// Square root operation
cout << "Enter a number: ";
cin >> num1;
cout << "Square root of " << num1 << " is " << sqrt(num1) << endl;
}
else
{
// For other operations
cout << "Enter two numbers: ";
cin >> num1 >> num2;
}
switch(operation)
{
case '+':
cout << "Result: " << num1 + num2 << endl;
break;

case '-':
cout << "Result: " << num1 - num2 << endl;
break;

case '*':
cout << "Result: " << num1 * num2 << endl;
break;

case '/':
if (num2 != 0)
cout << "Result: " << num1 / num2 << endl;
else
cout << "Error: Division by zero is undefined!" << endl;
break;

3
case '%':
if (static_cast<int>(num2) != 0)
cout << "Result: " << static_cast<int>(num1) % static_cast<int>(num2) << endl;
else
cout << "Error: Division by zero is undefined!" << endl;
break;

case '^':
cout << "Result: " << pow(num1, num2) << endl;
break;

default:
cout << "Error: Invalid operator!" << endl;
break;

}
}
return 0;
}

You might also like