In: Computer Science
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++
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