Question

In: Computer Science

Write a program that prompts user to enter integers one at a time and then calculates...


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.

Solutions

Expert Solution

(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:-


Related Solutions

JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
C++ Write a program that prompts the user to enter 50 integers and stores them in...
C++ Write a program that prompts the user to enter 50 integers and stores them in an array. The program then determines and outputs which numbers in the array are sum of two other array elements. If an array element is the sum of two other array elements, then for this array element, the program should output all such pairs separated by a ';'. An example of the program is shown below: list[0] = 15 is the sum of: ----------------------...
C Program 1. Write a program that prompts the user to enter 3 integers between 1...
C Program 1. Write a program that prompts the user to enter 3 integers between 1 and 100 from the keyboard in function main and then calls a function to find the average of the three numbers. The function should return the average as a floating point number. Print the average from main.The function header line will look something like this:float average(int n1, int n2, int n3) STOP! Get this part working before going to part 2. 2. Create a...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum...
Problem description Write a C++ program that prompts the user to enter two non-negative integers, firstNum and secondNum. The program then prints all palindromic primes (Links to an external site.) between firstNum and secondNum, inclusive. A palindromic number is a number whose digits are the same forward or backward (e.g., 12321). A palindromic prime is a prime number that is also a palindromic number (e.g., 101). You must implement and use the following functions as prototyped below: /// --------------------------------------------------------------- ///...
Write a program that prompts the user to enter a positive integer and then computes the...
Write a program that prompts the user to enter a positive integer and then computes the equivalent binary number and outputs it. The program should consist of 3 files. dec2bin.c that has function dec2bin() implementation to return char array corresponding to binary number. dec2bin.h header file that has function prototype for dec2bin() function dec2binconv.c file with main function that calls dec2bin and print results. This is what i have so far. Im doing this in unix. All the files compiled...
Problem 4 : Write a program that prompts the user to enter in an integer and...
Problem 4 : Write a program that prompts the user to enter in an integer and then prints as shown in the example below Enter an integer 5 // User enters 5 1 2 2 3 3 3 4 4 4 4 5 5 5 5 5 Bye
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
Write a program that prompts the user to enter a series of strings, but with each...
Write a program that prompts the user to enter a series of strings, but with each string containing a small integer. Use a while loop and stop the loop when the user enters a zero. When the loop has finished, the program should display: the number of user inputs (not counting the final zero input). the total of the integers in the strings entered. the average of the integers accurate to one decimal place. Any help is greatly appreciated, this...
Write a program that allows the user to enter two integers and a character If the...
Write a program that allows the user to enter two integers and a character If the character is A, add the two integers If it is S, subtract the second integer from the first else multiply the integers Display the results of the arithmetic
Write a test program that prompts the user to enter a sequence of numbers ending with...
Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input. Use inheritance and polymorphism approach. in java please
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT