Write code that uses any type of loop. The code should continually ask for a user input and sums all user inputs that are divisible by 2. The loop should continue until the user enters a negative number. The code should output the sum a single time once user entry has completed. You may assume all libraries and namespaces have been previously written into the code, you are just writing everything that would go inside the main function (beyond the return 0:).

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
### Loop and Condition-Based User Input Summation

#### Problem Statement:
Write a program using any type of loop that continually prompts the user for input and sums all user inputs that are divisible by 2. The loop should terminate when the user enters a negative number. Upon termination, the program should output the computed sum once.

#### Task Requirements:
- Implement a loop for continuous user input.
- Identify and sum only those inputs that are divisible by 2.
- Terminate the loop upon receiving a negative number input.
- Output the sum after loop termination.

#### Implementation Details:
- Assume all necessary libraries and namespaces are already included.
- Focus on the code within the main function, post the `return 0;` statement.

### Example Code:
Here is an illustrative example in C++:

```cpp
int main() {
    int sum = 0;
    int userInput;

    // Loop to continually ask for user input
    while (true) {
        std::cout << "Enter a number: ";
        std::cin >> userInput;

        // Check for negative number to terminate loop
        if (userInput < 0) {
            break;
        }

        // Check if the number is divisible by 2
        if (userInput % 2 == 0) {
            sum += userInput;
        }
    }

    // Output the computed sum
    std::cout << "The sum of all numbers divisible by 2 is: " << sum << std::endl;

    return 0;
}
```
##### Explanation of the Code:
1. **Initialization:**
   - `sum` is initialized to zero to store the cumulative total.
   - `userInput` is declared to capture the user's input.

2. **Loop:**
   - A `while (true)` loop is used for continuous execution until explicit termination.
   - Inside the loop:
     - Prompt the user for input.
     - Check if the input is negative to break the loop.
     - Verify if the input number is divisible by 2; if true, add it to `sum`.

3. **Output:**
   - After exiting the loop, the final sum is printed to the console.

This example demonstrates how to handle continuous user input, apply a conditional check, and compute a running total with loop termination based on specific criteria.
Transcribed Image Text:### Loop and Condition-Based User Input Summation #### Problem Statement: Write a program using any type of loop that continually prompts the user for input and sums all user inputs that are divisible by 2. The loop should terminate when the user enters a negative number. Upon termination, the program should output the computed sum once. #### Task Requirements: - Implement a loop for continuous user input. - Identify and sum only those inputs that are divisible by 2. - Terminate the loop upon receiving a negative number input. - Output the sum after loop termination. #### Implementation Details: - Assume all necessary libraries and namespaces are already included. - Focus on the code within the main function, post the `return 0;` statement. ### Example Code: Here is an illustrative example in C++: ```cpp int main() { int sum = 0; int userInput; // Loop to continually ask for user input while (true) { std::cout << "Enter a number: "; std::cin >> userInput; // Check for negative number to terminate loop if (userInput < 0) { break; } // Check if the number is divisible by 2 if (userInput % 2 == 0) { sum += userInput; } } // Output the computed sum std::cout << "The sum of all numbers divisible by 2 is: " << sum << std::endl; return 0; } ``` ##### Explanation of the Code: 1. **Initialization:** - `sum` is initialized to zero to store the cumulative total. - `userInput` is declared to capture the user's input. 2. **Loop:** - A `while (true)` loop is used for continuous execution until explicit termination. - Inside the loop: - Prompt the user for input. - Check if the input is negative to break the loop. - Verify if the input number is divisible by 2; if true, add it to `sum`. 3. **Output:** - After exiting the loop, the final sum is printed to the console. This example demonstrates how to handle continuous user input, apply a conditional check, and compute a running total with loop termination based on specific criteria.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 5 steps with 2 images

Blurred answer
Knowledge Booster
Function Arguments
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education