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.
JAVA Write a Java console application that prompts the user to enter the radius of a...
JAVA Write a Java console application that prompts the user to enter the radius of a circle, then prints its radius, diameter, circumference, and area. The Console Output Enter the radius of the circle: 1.2 The radius is 1.2 The diameter is 2.4 The circumference is 7.5398223686155035 The area is 4.523893421169302 Programmer Notes Write and document your program per class coding conventions. Add an instance variable double radius. Generate its get/set methods. Manually type the methods getDiameter(), getCircumference(), and getArea()....
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
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 compute xy
Write a java application that will read a set of values from the user and stores...
Write a java application that will read a set of values from the user and stores them in array a[]. The application should ask the user for the number of values he will enter. You need to do the following methods: 1. Read: This method will read n values from the user and store them in an array a 2. Print: this method will print the array as given in the sample output 3. maxEven: This method will find the...
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.
Write an application that asks the user for a natural number. Display all the natural numbers...
Write an application that asks the user for a natural number. Display all the natural numbers up to and including the user's input. Also display the sum of all those numbers. JAVA
In VB console write a Console Application that reads an input value for ??. You can...
In VB console write a Console Application that reads an input value for ??. You can assume that the user only inputs numeric values.
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT