Question

using c++ write a program that reads numbers from the user until the user enters a...

using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following:
1. Output the sum of all even numbers

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
using namespace std;

int main() {
    int num, sum = 0;
    while (true) {
        cout << "Enter a number: ";
        cin >> num;
        if(num == -999) break;
        if(num > 0 && num % 2 == 0) {
            sum += num;
        }
    }
    cout << "Sum of all even numbers is " << sum << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
using c++ write a program that reads numbers from the user until the user enters a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • using c++ write a program that reads numbers from the user until the user enters a...

    using c++ write a program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following:   must use loops and numbers to do this 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers

  • Write a program in C++ that reads in integer numbers from a file called scores.txt until...

    Write a program in C++ that reads in integer numbers from a file called scores.txt until the sentinel value -999 is read. The program should then output the total and average of the numbers read and output each of the numbers incremented by the overall average to a file called or scoresout.txt along with the sentinel value of -999 at the end So if 10, 20, 30 and -999 are read in then the program would display a total of...

  • In this exercise, write a complete Java program that reads integer numbers from the user until...

    In this exercise, write a complete Java program that reads integer numbers from the user until a negative value is entered. It should then output the average of the numbers, not including the negative number. If no non-negative values are entered, the program should issue an error message. You are required to use a do-while loop to solve this problem. Solutions that do not use a do-while loop will be given a zero. Make sure to add appropriate comments to...

  • Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done"...

    Exercise 1: Write a program which repeatedly reads numbers until the user enters "done". Once "done" is entered, print out the total, count, and average of the numbers. If the user enters anything other than a number, detect their mistake using try and except and print an error message and skip to the next number. Enter a number: 4 Enter a number: 5 Enter a number: bad data Invalid input Enter a number: 7 Enter a number: done 16 3...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. ​ 1 largest = None 2 smallest = None 3 while True: 4...

  • 5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters...

    5.2 Write a program that repeatedly prompts a user for integer numbers until the user enters 'done'. Once 'done' is entered, print out the largest and smallest of the numbers. If the user enters anything other than a valid number catch it with a try/except and put out an appropriate message and ignore the number. Enter 7, 2, bob, 10, and 4 and match the output below. 1 largest = None 2 smallest = None 3 while True: 4 num...

  • using c++ (a) Write a program that reads 10 numbers from the user as input and...

    using c++ (a) Write a program that reads 10 numbers from the user as input and loads them into an array.

  • C++ assignment Write a program that reads a sequence of integers and prints the following: 1....

    C++ assignment Write a program that reads a sequence of integers and prints the following: 1. The number of odd and even numbers of inputs Use a sentinel value to signal the end of inputs. If the sentinel value is the first you enter, give a message “NO input is entered!”. Input Validation: Do not accept a negative number.

  • 1. Write a program that reads a sequence of numbers from the keyboard, and displays the...

    1. Write a program that reads a sequence of numbers from the keyboard, and displays the smallest number. The sequence has at least one number and is terminated by -999(-999 will not appear in the sequence other than as the end marker). 2. Write a program which requests an integer input n from the keyboard and computes the sum of square of k for all k from 1 to n(inclusive). use java

  • Write a Python program that keeps reading in names from the user, until the user enters...

    Write a Python program that keeps reading in names from the user, until the user enters 0. Once the user enters 0, you should print out all the information that was entered by the user. Use this case to test your program: Input: John Marcel Daisy Samantha Nelson Deborah 0 ================ Output: John Marcel Daisy 25 Samantha Nelson Deborah Hints: Create an array Names to hold the input names. Use the Names.append(x) function to add a new name to the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT