Question

In: Computer Science

C++ while loop Exercise Write a program that continues to ask the user to enter any...

C++

while loop Exercise

Write a program that continues to ask the user to enter any set of numbers, until the user enters the number -1. Then display the total sum of numbers entered and their average. (note that you need to define a counter that counts how many numbers so the average = (sum/n) where n is your counter total.

#include <iostream>

using namespace std;

int main()

{

int number, n=0, sum=0;

cout << "Enter a number to start with " << endl;

cin >> number;

while (number != -1)

{

.

.

.

}

.

.

.

return 0;

}

Exercise 3: For loop

Modify Exercise 2 so that the user enters only 5 numbers, and the code display the total sum and their average.

Solutions

Expert Solution

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main()

{

int number, n=0, sum=0;
float average;
cout << "Enter a number to start with " << endl;
cin >> number;
while (number != -1)
{
sum = sum + number; // summing the numbers
n++; // increment the counter
cin >> number;
}

average = (float)sum / n; // calculate average
cout << "Average = " << average; // print average

return 0;

}

OUTPUT :


#include <iostream>
#include <bits/stdc++.h>
using namespace std;


int main()
{
int n = 5, sum = 0, a[5];
float average;
for(int i = 0; i < n; i++)
{
cin >> a[i]; // read the array
sum = sum + a[i]; // summing
}
cout << "Sum = " << sum << endl; // print the sum
cout << "Average = " << (float)sum / n; // print the average

return 0;

}

OUTPUT :


Related Solutions

Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Write a program that uses a while loop with a priming read to ask the user...
Write a program that uses a while loop with a priming read to ask the user to input a set positive integers. As long as the user enters a number greater than -1, the program should accumulate the total, keep track of the number of numbers being entered and then calculate the average of the set of numbers after the user enters a -1. This is a sentinel controlled-loop. Here is what a sample run should look like: Enter the...
​​​​​​​For java program. Write a while loop that will let the user enter a series of...
​​​​​​​For java program. Write a while loop that will let the user enter a series of integer values and compute the total values and number of values entered. An odd number will stop the loop. Display the number of iterations and the total of the values after the loop terminates. for Loop Write a for loop to display all numbers from 13 - 93 inclusive, ending in 3. Write a for loop to display a string entered by the user...
Write a program in PYTHON, using a while loop, that asks the user to enter the...
Write a program in PYTHON, using a while loop, that asks the user to enter the amount that they have budgeted for the month. The program should then prompt the user to enter their expenses for the month. The program should keep a running total. Once the user has finished entering their expenses the program should then display if the user is over or under budget. The output should display the monthly budget, the total expenses and whether the user...
Write a python program that will ask the user to enter any number of days. The...
Write a python program that will ask the user to enter any number of days. The program reads the number of days from the user, and returns how many years, months, and days are there in the given number of days. Your program should prompt the user to: Enter the number of days : 1152 The user types number of days (1152 above, for example) and hit enter key. The program then will print: 3 years 1 months 27 days...
Write a complete program using the do-while loop that prompts the user to enter a string...
Write a complete program using the do-while loop that prompts the user to enter a string and displays its first and last characters. Repeat until the user enter a string that contains only one character. Here is an example to show you how the output may look like: <output> Enter a string ( type only one character to exit): this is my string The first character is t The last character is g Enter a string ( type only one...
Using C++ Write a program to ask user to enter a password and validity the password....
Using C++ Write a program to ask user to enter a password and validity the password. The password should be 8 – 20 characters long (8 is included and 20 is included), it should contain at least one uppercase letter and one lower case letter. The password should contain at least one digit, and at least one symbol other than alphabets or numbers. Most importantly, the password may not contain any white space. Functions: You will need at least the...
Write a C++ program to ask the user to enter the shape type: square, sphere or...
Write a C++ program to ask the user to enter the shape type: square, sphere or circle and the appropriate dimensions of the shape.. The program should output the following information about the shape: a.for a square. it output the area and perimeter. b. for a sphere, it outputs the area. c. fir a circle, it outputs the volume. if the user input anything else, your program should output: "the program does not recognize this shape". Use const whenever it...
Write a program that will ask the user to enter the amount of a purchase. The...
Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 5 percent and the county sales tax is 2.5 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer...
Use a while(true) loop to ask the user to “Enter a non-negative integer (enter negative integer to quit):” and store this into an int named n. If the user enters a negative int for n, the while loop is broken via the brake statement. Otherwise, in the remaining part of the while loop, use a for loop to compute the sum of the inverse factorials from 0 to n, that is sum = 1/0! + 1/1! + 1/2! + ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT