You are required to develop a C++ program which simulates a simple calculator. T
ID: 671757 • Letter: Y
Question
You are required to develop a C++ program which simulates a simple calculator. The calculator is capable of taking (reading interactively) three real numbers and performs the following operations:
Finding the sum and print the result
Finding the average and pint the result
Finding the largest and print the result
Finding the smallest and print the result
The user determines which of these operations to be performed.
Program requirements:
- The program should ask user for appropriate choice and required data.
- The use should have an option to provide his/her choice or terminate the program.
- The program must contain at least four functions using parameters. These functions should perform the above operations.
- Program should use a class that contains the above functions.
- The program must be fully documented.
- You must submit a hard copy of the source and a properly labeled output.
- Test your program for different values for the real numbers.
Explanation / Answer
#include <iostream>
using namespace std;
int main ()
{
int choice;
while(1){
cout<<"1.Finding the sum and print the result"<<endl;
cout<<"2.Finding the average and pint the result"<<endl;
cout<<"3.Finding the largest and print the result"<<endl;
cout<<"4.Finding the smallest and print the result"<<endl;
cout<<"5. Exit"<<endl;
cin>>choice;
if(choice == 5)
break;
double a[3],sum=0;
double largest=100000;
double smallest=0;
switch(choice){
case 1 :
cout<<"Enter three numbers";
for(int i = 0 ; i < 3 ; i++){
cin>>a[i];
sum += a[i];
}
cout<<"Sum :"<<sum<<endl;
break;
case 2 :
cout<<"Enter three numbers";
for(int i = 0 ; i < 3 ; i++){
cin>>a[i];
sum += a[i];
}
cout<<"Average :"<<sum/3<<endl;
break;
case 3:
cout<<"Enter three numbers";
for(int i = 0 ; i < 3 ; i++){
cin>>a[i];
if(largest > a[i])
largest = a[i];
}
cout<<"Largest :"<<largest<<endl;
break;
case 4:
cout<<"Enter three numbers";
for(int i = 0 ; i < 3 ; i++){
cin>>a[i];
if(smallest < a[i])
smallest = a[i];
}
cout<<"Smallest :"<<smallest<<endl;
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.