Question

In: Computer Science

Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and...

  1. Write a C++ arithmetic calculator program that performs four arithmetic operations namely addition, subtraction, multiplication and division. This program prompts user to enter data in the following order: First data is the number, second data is an operator and the third data is the number. You are required to:
  1. Write a function for addition.
  2. Write a function for subtraction.
  3. Write a function for multiplication.
  4. Write a function for division.
  5. Write a main program which calls four functions based on the operator selected by user.

Solutions

Expert Solution

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!


Related Solutions

C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should...
C -Language Create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should prompt the user for the operation they wish to perform followed by the numbers they wish to operate on. You should have a function for each operation and use branches to determine which function to call. I need this to make any integers given, into decimal numbers, such as 3 to 3.0, or 2 to 2.0, also, so that I can multiply or add...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication,...
In Visual Studio in C#, you will create a simple calculator that performs addition, subtraction, multiplication, and division. Your program should request a numerical input from the user, followed by the operation to be performed, and the second number to complete the equation. The result should be displayed to the user after each equation is performed. For example, if the user performs 3+3, the program should display 6 as the result. The program should continue running so the user can...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The...
Write a Python program that will perform various calculations (addition, subtraction, multiplication, division, and average). The program will add, subtract, multiply, or divide 2 numbers and provide the average of multiple numbers inputted from the user. You need to define a function named performCalculation which takes 1 parameter. The parameter will be the operation being performed (+,-,*,/). This function will perform the given prompt from the user for 2 numbers then perform the expected operation depending on the parameter that’s...
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers....
Write a Java program to print the sum (addition), multiplication, subtraction, and division of two numbers. Start your code by copying/pasting this information into an editor like notepad, notepad++, or IDLE: public class Main { public static void main(String[] args) { // Write your code here } } Sample input: Input first number: 125 Input second number: 24 Sample Output: 125 + 24 = 149 125 - 24 = 101 125 x 24 = 3000 125 / 24 = 5...
(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions....
(Fraction calculator c++) | Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a/b, in which _a_ and _b_ are integers and b ≠ 0. Your program must be menu driven, allowing the user to select the operation (+, –, *, or /) and input the numerator and denominator of each fraction. Furthermore, your program must consist of at least the following functions: Function menu: This function informs the user about the...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in...
There are four basic functions in math. They are addition, subtraction, multiplication and division. Children in 1st grade focus on addition. They are required to memorize their addition facts from 1 to 12. Create a function that prompts the user to enter a number between 1 and 12. Then display - using the input supplied - the addition and subtraction math facts for that number from 1 to 12 for the number entered. The output should be the results of...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The...
Write a C++ program to perform two-4 bit binary number operations including addition and subtraction. The user will type in two-4 bit binary numbers with the selection of one of the operations. Then, the program will calculate the result of the calculation. Display two-4 bit binary numbers and the result from the calculation.
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions.
Fraction calculator: Write a program that lets the user perform arithmetic operations on fractions. Fractions are of the form a / b, in which a and b are integers and b != 0.Your program must:be menu driven, allowing the user to select the operation (+, -, *, or /) and input the numerator and denominator of each fraction.The input must be validated for correct operation (+, -, *, or /) and b != 0. If any of these cases are...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication,...
Java program - you are not allowed to use arithmetic operations such as division (/), multiplication, or modulo (%) to extract the bits. In this exercise use only logic bit-wise operations. Write a program that prompts the user to enter a positive integer n (0 up to 232 -1). You must write a function that takes as input n and returns a string s representing the number n in binary. For this assignment, you CANNOT use the arithmetic division by...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT