In: Computer Science
|
|
According to the problem statement above,
I have coded the function using C++
Below are the Snippets:
Code Snippet:
Output Snippet:
Code in Text format:
#include <iostream>
// including libraries
using namespace std;
float addition(float a, float b)
//
addition function
{
return a + b;
// returning addition
}
float subtraction(float a, float b)
//
subtraction function
{
return a - b;
// returing subraction
}
float multiplication(float a, float b)
//
multiplication function
{
return a * b;
// returning multiplication
}
float division(float a, float b)
//
division function
{
return a / b;
// returning division
}
int main()
// main function
{
char op;
// char to scan
operator
float a, b;
// a and b to scan
numbers
cout << "Enter Number1: ";
// prompting to enter
number1
cin >> a;
// scanning number1
cout << "Enter Operator: ";
// prompting to enter
operator
cin >> op;
// scanning operator
cout << "Enter Number2: ";
// prompting to enter
number2
cin >> b;
// scanning number2
switch(op)
// switch by operator
{
case '+':
// if operator +
cout << "Addition: " << addition(a,b) <<
endl;
// passing to addition
function
break;
// break
statement
case '-':
// if operator -
cout << "Subtraction: " << subtraction(a,b) <<
endl; // passing
to subtraction function
break;
// break
statement
case '*':
// if operator *
cout << "Multiplication: " << multiplication(a,b)
<< endl; // passing to
multiplication function
break;
// break statement
case '/':
// if operator /
cout << "Division: " << division(a,b) <<
endl;
// passing to division
function
break;
// break
statement
default:
// if operator doesn't match
with any
cout << "Error! operator is not correct" << endl;
break;
// break
statement
}
return 0;
}
I hope the above snippets, information, and the explanation will help you out!
Please comment if you have any queries/errors!
Thank you!