Question

In: Computer Science

Write a console application to total a set of numbers indicated and provided by a user,...

Write a console application to total a set of numbers indicated and provided by a user, using a while loop or a do…while loop according to the user’s menu choice as follows:

C++programming

Menu

1. To total numbers using a while loop

2. To total numbers using a do...while loop

Enter your menu choice: 1

How many numbers do you want to add: 2

Enter a value for number 1: 4

Enter a value for number 2: 5

The total is: 9

Example

Menu

1. To total numbers using a while loop

2. To total numbers using a do...while loop.

Enter your menu choice: 1

How many numbers do you want to add: 4

Enter a value for number 1: 3

Enter a value for number 2: 2

Enter a value for number 3: 5

Enter a value for number 4: 1

The total is: 11

Requirements

  1. All the values above are only examples.
  1. Menu choice 1 will total numbers provided by the user using a while loop.
  2. Menu choice 2 will total numbers provided by the user using a do…while loop.
  3. The message “Enter a value for number #” must include a sequence number (i.e. 1, 2, 3, 4, 5, 6, …).

Solutions

Expert Solution

Program:

#include <iostream>

using namespace std;

int
main ()
{
  int menuChoice, numbersCount;
  // Display menu
  cout << "Menu" << endl;
  cout << "1. To total numbers using a while loop" << endl;
  cout << "2. To total numbers using a do...while loop" << endl;
  // Take input for menu choice
  cout << "Enter menu choice: ";
  cin >> menuChoice;
  // Take input for how many numbers to be added
  cout << "How many numbers do you want to add: ";
  cin >> numbersCount;
  
  int arr[numbersCount];
  // Take input for numbers and add them to an array
  for (int i = 0; i < numbersCount; ++i) {
      cout << "Enter a value for number " << i+1 << ": ";
      cin >> arr[i];
  }
  // condition to check the menu choice is 1
  if (menuChoice == 1) {
      int sum = 0;
      int n = 0;
      // Sum the numbers using while loop
      while(n < numbersCount) {
                sum += arr[n];
                n++;
      }
      // Printing the sum of the elements
      cout << "The total is: " << sum;
  }
  // condition to check the menu choice is 2
  else if (menuChoice == 2) {
      int sum = 0;
      int n = 0;
      // Sum the numbers using do...while loop
      do {
        sum += arr[n];
                n++;
      }
    while (n < numbersCount);
    // Printing the sum of the elements
    cout << "The total is: " << sum;
  }

  return 0;
}

Output:


Related Solutions

Write a C++ console application that allows your user to enter the total rainfall for each...
Write a C++ console application that allows your user to enter the total rainfall for each of 12 months into an array of doubles. The program should validate user input by guarding against rainfall values that are less than zero. After all 12 entries have been made, the program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts.
C# Create a console application that asks the user for two numbers in the range 0-255...
C# Create a console application that asks the user for two numbers in the range 0-255 and then divides the first number by the second: Enter a number between 0 and 255: 100 Enter another number between 0 and 255: 8 100 divided by 8 is 12 Enter a number between 0 and 255: apples Enter another number between 0 and 255: bananas FormatException: Input string was not in a correct format.
Write a MIPS program that will ask the user to enter two numbers at the console...
Write a MIPS program that will ask the user to enter two numbers at the console and pass the values to a function that does multiplication
in java Write an application that gets two numbers from the user and prints the sum,...
in java Write an application that gets two numbers from the user and prints the sum, product, difference and quotient of the two numbers in a GUI.
C# Create a console application that prompts the user to enter a regular expression, and then...
C# Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a regular expression...
Language: C# Create a new Console Application. Your Application should ask the user to enter their...
Language: C# Create a new Console Application. Your Application should ask the user to enter their name and their salary. Your application should calculate how much they have to pay in taxes each year and output each amount as well as their net salary (the amount they bring home after taxes are paid!). The only taxes that we will consider for this Application are Federal and FICA. Your Application needs to validate all numeric input that is entered to make...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression,...
C# & ASP.NET Create a console application that prompts the user to enter a regular expression, and then prompts the user to enter some input and compare the two for a match until the user presses Esc: The default regular expression checks for at least one digit. Enter a regular expression (or press ENTER to use the default): ^[a- z]+$ Enter some input: apples apples matches ^[a-z]+$? True Press ESC to end or any key to try again. Enter a...
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from...
Prompt user to enter 2 integer numbers from console, compare and display these 2 numbers from small to great. This is required to be done in MIPS assembly language.
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until...
Python Jupiter Notebook Write a program that keeps getting a set of numbers from user until the user enters "done". Then shows the count, total, and average of the entered numbers. Example: Enter a number: 55 Enter a number: 90 Enter a number: 12 Enter a number: done You entered 3 numbers, total is 157, average is 52.33333
Create an application that allows the user to enter the total for an order and the...
Create an application that allows the user to enter the total for an order and the name of the customer. If the order is less than 500 dollars, the customer gets no discount. If the order is greater than or equal to 500 and less than 1000 dollars, the customer gets a 5 percent discount. If the order is greater than or equal to 1000 dollars, the customer gets a 10 percent discount. The application should display the name of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT