In: Computer Science
For this assignment you will develop pseudocode and write a C++ program for a simple calculator. You will create both files in Codio. Put your pseudocode and C++ code in the files below.
PSEUDOCODE FILE NAME: Calculator.txt
C++ SOURCE CODE FILE NAME : Calculator.cpp
DESCRIPTION:
Write a menu-driven program to perform arithmetic operations and computations on a list of integer input values. Present the user with the following menu. The user will choose a menu option. The program will prompt the user for 2 whole numbers (integers), perform the operation specified by the menu option and then display the result. Name your file Calculator.cpp.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice:
Suppose the user chooses option (A). The program will prompt the user for 2 numbers, add them and display the sum, then thank the user.
Enter first number: 3
Enter second number 8
The sum is 11
Thank you for using the Calculator.
The program ends after giving the result.
Supposed the program is run again and the user enters option S. The program will subtract the second number from the first and present the result.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: S
Enter first number: 4
Enter second number: 6
The difference is -2
Thank you for using the Calculator.
Option M will multiply the 2 numbers. Option D will divide the first number by the second. Option R will produce the remainder after dividing the first number by the second. If the user chooses (Q) the program exits with a message : “is turned off”
If the user enters an illegal option, the program should show an error message then end. For example.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: G
invalid choice
NOTE:
Your menu should appear exactly as it is shown above.
The prompts for the user and the output for the results should appear exactly as specified above.
The menu options are case insensitive. The Calculator should accept upper-case ‘M’ and lower-case 'm’.
Account for invalid operations such as dividing a number by zero or attempting to get the remainder after dividing by zero. In the event that happens print “The remainder is Error”
More Examples
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: a
Enter first number: 5
Enter second number: 4
The sum is 9
Thank you for using the Calculator.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: R
Enter first number: 8
Enter second number: 10
The remainder is 8
Thank you for using the Calculator.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: m
Enter first number: 3
Enter second number: 2
The product is 6
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: D
Enter first number: 5
Enter second number: 0
The division is Error
Thank you for using the Calculator.
MY CALCULATOR
A) Add
S) Subtract
M) Multiply
D) Divide
R) Remainder (Mod)
Q) QUIT (turn off)
Enter Menu Choice: Q
is turned off
Thank you for using the Calculator.
The pseudo code and the full cpp code for the following problem is given below.
In the simple arithmetic calculator
The pseudocode for sum is.
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1+num2; //add two number and display it
cout<<"The sum is "<<result<<endl;
The pseudo code for subtraction is.
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1-num2; //difference store in result and displa
it
cout<<"The difference is "<<result<<endl;
The pseudo code for multiplication is.
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1*num2; //multiply both no. and display it
cout<<"The product is "<<result<<endl;
The pseudo code for division is.
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
if(num2==0){ //if num 2 is 0
cout<<"The division is Error"<<endl; //display the
error mssg
}
else{
int result=num1+num2;//else divided two number and display it
cout<<"The divisiom is "<<result<<endl;
}
The pseudo code for reminder is.
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1%num2; //calculating reminder and display it
cout<<"The reminder is "<<result<<endl;
These are the following pseudo codes.
The full cpp program with output is given below.
CODE.
#include <iostream>
using namespace std;
int main()
{
//driver code to display menu
cout << "MY CALCULATOR" << endl;
cout<<"A) Add"<<endl;
cout<<"S) Subtract"<<endl;
cout<<"M) Multiply"<<endl;
cout<<"D) Divide"<<endl;
cout<<"R) Remainder(Mod)"<<endl;
cout<<"Q) Quit(turn off)"<<endl;
cout<<"Enter Menu Choice: ";
char x; //initialize char variable to take user response
cin>>x;
if(x=='A' || x=='a') //if user opt for addition the statement is
correct
{
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1+num2; //add two number and display it
cout<<"The sum is "<<result<<endl;
}
//condition for the user input for substraction
else if(x=='S' || x=='s')
{
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1-num2; //difference store in result and displa
it
cout<<"The difference is "<<result<<endl;
}
//condition for user input for multiplication
else if(x=='M' || x=='m')
{
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1*num2; //multiply both no. and display it
cout<<"The product is "<<result<<endl;
}
//condition for user opt for division
else if(x=='D' || x=='d')
{
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
if(num2==0){ //if num 2 is 0
cout<<"The division is Error"<<endl; //display the
error mssg
}
else{
int result=num1+num2;//else divided two number and display it
cout<<"The divisiom is "<<result<<endl;
}
}
//condition for user opt for reminder
else if(x=='R' || x=='r')
{
int num1,num2;
cout<<"Enter first number: ";
cin>>num1;
cout<<"Enter second number: ";
cin>>num2;
int result=num1%num2; //calculating reminder and display it
cout<<"The reminder is "<<result<<endl;
}
//condition for if the user want to quit.
else if(x=='Q' || x=='q'){
cout<<"is turned off"<<endl;
}
else{
cout<<"Invalid choice"<<endl;
}
cout<<"Thank you for using Calculator."; //thank you
message
return 0;
}
SNAPS.
OUTPUT.
For any query message me in comment.