In: Computer Science
Write a program that prompts user to enter integers one at a time
and then calculates and displays the average of numbers entered.
Use a while loop and tell user that they can enter a non-zero
number to continue or zero to terminate the loop.
(Switch statement) Write a program that prompts user to enter two
numbers x and y, and then prompts a short menu with following 4
arithmetic operations:
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Based on the user’s choice display the right result, for example if
user choses 3, display x * y =z. Program should allow user to keep
making choices from the menu till they enter 0 to exit. It's a c++
program.
(1.) The C++ code is as follows:-
#include<iostream>
using namespace std;
int main()
{
int i,sum=0,c=0; //decleration and initialization of
variables
float avg;
cout<<"Enter non-zero number to continue,(0 to
exit)"<<endl;
cin>>i; //input first integer number
while(i>0) //loop until user gives input 0
{
sum+=i; //add all the numbers
c++; //count the number entered bu user
cout<<"Enter non-zero number to continue,(0 to
exit)"<<endl;
cin>>i;
}
avg=sum/c; //calculate average
cout<<"Average is: "<<avg; //display average
return 0;
}
The output of the above code is as follows:-
Enter non-zero number to continue,(0 to exit)
13
Enter non-zero number to continue,(0 to exit)
45
Enter non-zero number to continue,(0 to exit)
24
Enter non-zero number to continue,(0 to exit)
76
Enter non-zero number to continue,(0 to exit)
58
Enter non-zero number to continue,(0 to exit)
0
Average is: 43
The screenshot of the above code is as follows:-
(2.) The C++ code is as follows:-
# include <iostream>
using namespace std;
int main()
{
int ch,x,y,z;
cout<<"Enter two operands: ";
cin>>x>>y;
cout<<"Chose 1 for addition"<<endl;
cout<<"Chose 2 for subtraction"<<endl;
cout<<"Chose 3 for multiplication"<<endl;
cout<<"Chose 4 for division"<<endl;
cout<<"Chose 0 to exit"<<endl;
cout<<"Enter option"<<endl;
cin>>ch;
do
{
switch(ch)
{
case 1:
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl;
break;
case 2:
z=x-y;
cout<<x<<"-"<<y<<"="<<z<<endl;
break;
case 3:
z=x*y;
cout<<x<<"*"<<y<<"="<<z<<endl;
break;
case 4:
z=x/y;
cout<<x<<"/"<<y<<"="<<z<<endl;
break;
default:
cout<<"Wrong choice"<<endl;
break;
}
cout<<"Chose 1 for addition"<<endl;
cout<<"Chose 2 for subtraction"<<endl;
cout<<"Chose 3 for multiplication"<<endl;
cout<<"Chose 4 for division"<<endl;
cout<<"Chose 0 to exit"<<endl;
cout<<"Enter option"<<endl;
cin>>ch;
}while(ch!=0);
return 0;
}
The output of the above code is as follows:-
Enter two operands: 10 2
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Enter option
1
10+2=12
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Enter option
2
10-2=8
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Enter option
3
10*2=20
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Enter option
4
10/2=5
Chose 1 for addition
Chose 2 for subtraction
Chose 3 for multiplication
Chose 4 for division
Chose 0 to exit
Enter option
0
The screenshot of the above code is as follows:-