Question

In: Computer Science

Your Task: Write a calculator Program with following functions (lack of function will result in a...

Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user.


1 - Add

2 - Subtract

3 - Multiply

4 - Divide

5 - Raise X to the power Y

6 - Finds if a number is even or odd

0 - Quit

Your calculator must:
1. Display a menu with all the options

2- Ask the user to select an option from the menu

3. Based on the user selection, your program must then ask for the operand/s and call the pertaining function.

4. Your program will accept int and the result can be displayed in double (utilize type casting).

5.Once your program is done with the user's option, your program must ask if the user would like to try another option utilizing loops)

Input Validation: Your program should only accepts numbers.

Language: C++


Solutions

Expert Solution

CODE FOR THE FOLLOWING PROGRAM:-

#include <iostream>
#include <cstdlib>
#include <math.h> 

using namespace std;

//Function to add two numbers
double Add(int first,int second){
    return first+second;
}

//Function to subtract two numbers
double Subtract(int first,int second){
    return first-second;
}

//Function to Multiply two numbers
double Multiply(int first, int second){
    return first*second;
}

//Function to divide two numbers
double Divide(int first,int second){
    return first/second;
}

//Function to Raise X to the power Y
double Power(int num,int Raise){
    return pow(num,Raise);
}

//Function to find whether the number is odd or even
string OddEven(int num){
    if(num%2==0){
        return "Even";
    }else{
        return "Odd";
    }
}

int main()
{
    //Variables used in the code
    int choice,a,b,num,power;
    
    do{
        //Menu to choose for the user
        cout<<"MENU\n";
        cout<<"1 - Add \n";
        cout<<"2 - Subtract\n";
        cout<<"3 - Multiply\n";
        cout<<"4 - Divide\n";
        cout<<"5 - Raise X to the power Y\n";
        cout<<"6 - Finds if a number is even or odd\n";
        cout<<"0 - Quit\n";
        cout<<"Enter your choice\n";
        cin>>choice;
        
        //If the user enters choice as 1
        if(choice==1){
            cout<<"Enter first number\n";
            while(!(cin>>a)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
             cout<<"Enter second number\n";
            while(!(cin>>b)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"The sum is: "<<Add(a,b)<<endl;
        }
        //If the user enters choice as 2
        else if(choice==2){
             cout<<"Enter first number\n";
            while(!(cin>>a)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
             cout<<"Enter second number\n";
            while(!(cin>>b)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"The Subtract is: "<<Subtract(a,b)<<endl;   
        }
        //If the user enters choice as 3
        else if(choice==3){
           cout<<"Enter first number\n";
            while(!(cin>>a)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
             cout<<"Enter second number\n";
            while(!(cin>>b)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"The multiply is: "<<Multiply(a,b)<<endl;
        }
        //If the user enters choice as 4
        else if(choice==4){
            cout<<"Enter first number\n";
            while(!(cin>>a)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
             cout<<"Enter second number\n";
            while(!(cin>>b)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"The divide is: "<<Divide(a,b)<<endl;
        }
        //If the user enters choice as 5
        else if(choice==5){
            cout<<"Enter the number whose power you want to raise\n";
            while(!(cin>>num)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"Enter the power to which you want to raise the number\n";
            while(!(cin>>power)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<num<<" to the power "<<power<<" is :"<<Power(num,power)<<endl;
        }else if(choice==6)
        //If the user enters choice as 6
        {
            cout<<"Enter the number \n";
            while(!(cin>>num)){
                cout<<"Must be a number: "<<endl;
                cin.clear();
                cin.ignore(100,'\n');
            }
            cout<<"The number you entered is :"<<OddEven(num)<<endl;
        }
        ////If the user wants to quit
        else if(choice==0){
            exit(0);
             }
        }while(choice!=0);
    return 0;
}

SCREENSHOT OF THE CODE AND SAMPLE OUTPUT:-

SAMPLE OUTPUT:-

HAPPY LEARNING


Related Solutions

Your Task: Write a calculator Program with following functions (lack of function will result in a...
Your Task: Write a calculator Program with following functions (lack of function will result in a grade of zero). Your program must have the following menu and depending on the users choice, your program should be calling the pertaining function to display the result for the user. 1 - Add 2 - Subtract 3 - Multiply 4 - Divide 5 - Raise X to the power Y 6 - Finds if a number is even or odd 0 - Quit...
Take the following program and include overload functions into it. Display result of function. C++ Program:...
Take the following program and include overload functions into it. Display result of function. C++ Program: #include <iostream> using namespace std; // this is the additional function string read() {     string input;     cout << "Enter input: ";     cin >> input;     return input; } int main() {     // call function here     string output = read();     cout << "You entered: " << output; }
Write a program that acts as a basic calculator Assume the starting result is 0 Ask...
Write a program that acts as a basic calculator Assume the starting result is 0 Ask the user for the operation to be performed 0 – Exit 1 – Add 2 – Subtract 3 – Multiply 4 – Divide (Assume input will not be 0) Keep on performing the operation and showing the result until the user inputs the ‘0’ (exit) operation. You need to have everything written as functions E.g. - You should only have the options listed once...
write C++ program using functions (separate function for each bottom) Write a program to find if...
write C++ program using functions (separate function for each bottom) Write a program to find if a number is large word for two given bottom base - bottom1 and bottom2. You can predict that a number, when converted to any given base shall not exceed 10 digits. . the program should ask from user to enter a number that it should ask to enter the base ranging from 2 to 16 after that it should check if the number is...
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt...
Write a program that contains 2 functions. Program will call a function named calc_commission that prompt the user to enter the sales amount and computes and prints with a description the commission paid to salesperson as follows: 10% for sales amount less than $2,000.00, 15% for sales amount less than $10,000.00 and 20% for sales amount less than $20,000.00, then function calc_commission calls another function name assign_base_salary() to ask the user to enter each of 5 salesperson’s base salary ,...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A...
Need to create Mortgage Calculator Program In C++ Modify your mortgage to include two functions. A function to get principal, rate, and term. A function to calculate monthly payment. Test your program by getting the data from the user by using the first function and calculate monthly payment by calling the other function. Add version control and write proper comments with a few blank lines & indentations in order to improve your programming style.
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
Change Calculator Task: Create a program that will input a price and a money amount. The...
Change Calculator Task: Create a program that will input a price and a money amount. The program will then decide if there is any change due and calculate how much change and which coins to hand out. Coins will be preferred in the least number of coins (starting with quarters and working your way down to pennies). Input: total_price, amount_tender allow the user to input 'q' for either value if they want to quit the program Validate: total_price cannot be...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT