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 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
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
Write a Java console application that reads a string for a date in the U.S. format...
Write a Java console application that reads a string for a date in the U.S. format MM/DD/YYYY and displays it in the European format DD.MM.YYYY  For example, if the input is 02/08/2017, your program should output 08.02.2017
Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application...
Write a console application called PlayingCards that has 4 classes: PlayingCards, Deck, Cards, Card. This application will be able to create a deck of cards, shuffle it, sort it and print it. Use the following class diagrams to code your application. Use the following code for the PlayingCards class public class PlayingCards{ public static void main(String[] args){                 System.out.println("Playings Cards");                 Deck deck = new Deck();                 deck.create();                 System.out.println("Sorted cards");                 deck.sort();                 deck.showCards();                 System.out.println("Shuffled cards");                 deck.shuffle();...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user...
C# Programming Language Write a C# program ( Console or GUI ) that prompts the user to enter the three examinations ( test 1, test 2, and test 3), homework, and final project grades then calculate and display the overall grade along with a message, using the selection structure (if/else). The message is based on the following criteria: “Excellent” if the overall grade is 90 or more. “Good” if the overall grade is between 80 and 90 ( not including...
Write a C++ console program that prompts a user to enter information for the college courses...
Write a C++ console program that prompts a user to enter information for the college courses you have completed, planned, or are in progress, and outputs it to a nicely-formatted table. Name the CPP as you wish (only use characters, underscores and number in your file name. DO NOT use blank). Its output should look something like this: Course Year Units Grade ---------- ---- ----- ----- comsc-110 2015 4 A comsc-165 2016 4 ? comsc-200 2016 4 ? comsc-155h 2014...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT