Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that mimics a calculator. The program should take as input two i

ID: 3849162 • Letter: W

Question

Write a program that mimics a calculator. The program should take as input two integers and the operation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) Some sample outputs follow:

3 + 4 = 7
13 * 5 = 65

Turn in two versions, the first version uses if..else statements, the second version uses a switch statement.

need:

source code for both programs with

IPO chart incorporated as comments after the name of the file

A test plan or a screen shot of the results of your testing, Use the same data for both versions, they should produce the same output with the same test data.

Explanation / Answer

//this code is successfully compiled and executed in Microsoft Visual Studio 2010.
#include<stdio.h>
#include<conio.h>
#include<iostream>
using namespace std;

int main(){
   int ch;
   float a,b,result;
   char con;
  
   do{
   cout<<"Enter 2 numbers ";
   cin>>a>>b;
   cout<<"Enter your choice 1.Addition 2.Subtraction 3.Multiplication 4.Division ";
   cin>>ch;
   switch(ch){
   case 1:
       result=a+b;
       cout<<"Result is "<<result;
       break;
   case 2:
       result=a-b;
       cout<<"Result is "<<result;
       break;
   case 3:
       result=a*b;
       cout<<"Result is "<<result;
       break;
   case 4:
       if(b==0)
           cout<<"Çannot divide by 0 ";
       else{
           result=a/b;
           cout<<"Result is "<<result;}
       break;
   }
   cout<<"Do you want to continue?(Y/N) ";
   cin>>con;
   }while(con=='Y'||con=='y');
     
   return 0;
}

Ananlysis:

Below are some sample input and output as an analysis of the code.

Enter 2 numbers
2
3
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
1

Result is 5
Do you want to continue?(Y/N)
y
Enter 2 numbers
5
6
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
2

Result is -1
Do you want to continue?(Y/N)
y
Enter 2 numbers
20
4
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
2

Result is 16
Do you want to continue?(Y/N)
y
Enter 2 numbers
20
4
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
3

Result is 80
Do you want to continue?(Y/N)
y
Enter 2 numbers
23
0
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
4

annot divide by 0

Do you want to continue?(Y/N)
y
Enter 2 numbers
56
14
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
4

Result is 4
Do you want to continue?(Y/N)
y
Enter 2 numbers
23
6
Enter your choice
1.Addition
2.Subtraction
3.Multiplication
4.Division
4

Result is 3.83333
Do you want to continue?(Y/N)
n

Algorithm:

1. Begin.
2. Decalre variables a,b, ch, con, result
3. Read 2 variables to be added and a choice of operation
4. if ch==1
add a and b and store in result
   if ch==2
subtract a from b and store in result
if ch==3
multiply a and b and store in result
   if ch==4
if b==0
   display the message that cannot divide by zero
   else
   divide a by b and store in result
5. Stop

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote