Question

C++ Write a loop that reads positive integers from standard input and that terminates when it...

C++

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out the sum of all the even integers read and the sum of all the odd integers read(The two sums are separated by a space). Declare any variables that are needed.

0 0
Add a comment Improve this question Transcribed image text
Answer #1


#include <iostream>

using namespace std;

int main() {
    int even_sum = 0, odd_sum = 0, num;
    while (true) {
        cin >> num;
        if (num <= 0) {
            break;
        }
        if (num % 2 == 0) {
            even_sum += num;
        } else {
            odd_sum += num;
        }
    }
    cout << even_sum << " " << odd_sum << endl;
    return 0;
}
Add a comment
Know the answer?
Add Answer to:
C++ Write a loop that reads positive integers from standard input and that terminates when it...
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
  • Please use Python to solve this problem: Write a loop that reads positive integers from standard...

    Please use Python to solve this problem: Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, on a single line and separated by a single space, the sum of all the even integers read and the sum of all the odd integers read.

  • python 3 please, for these questions you do not need to write the whole code just...

    python 3 please, for these questions you do not need to write the whole code just the expression 1)Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate line, The loop terminates when it reads an integer that is not positive. 2) Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop...

  • 1. Write a for loop that prints the sum of all positive even integers less than...

    1. Write a for loop that prints the sum of all positive even integers less than 200. Assume that variables i and sum are already declared. 2. Write a while loop that prints the sum of all positive odd integers less than 100. Assume that variables i and sum are already declared. 3. Write a do-while loop that prints the sum of all positive multiples of 3 less than or equal to 150. Assume that variables i and sum are...

  • Write a fragment of code that reads in a header value from the standard input and...

    Write a fragment of code that reads in a header value from the standard input and then reads in that many integers (also from standard input) and prints their sum to standard output (for loops, header values, accumulators, basic arithmetic). Declare any variables you use. part b Objects of the Calculator class require no additional information when created. Define an object named calc, of type Calculator.

  • Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n...

    Python My ITlab: 1. Write some code that repeatedly reads a value into the variable n until a number between 1 and 10 (inclusive) has been entered. 2. Write some code that repeatedly reads a value from standard input into the variable response until at last a Y or y or N or n has been entered. 3.Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, each on a separate...

  • Write a C program that reads a list of positive integers from a file named "input.txt."...

    Write a C program that reads a list of positive integers from a file named "input.txt." and count number of odd integers and even integers and prints the results to another file called "results.txt". Please submit both the input and results files along with the c program.

  • C++ 3. Write a program that reads integers from a file, sums the values and calculates...

    C++ 3. Write a program that reads integers from a file, sums the values and calculates the average. a. Write a value-returning function that opens an input file named in File txt. You may "hard-code" the file name, i.e., you do not need to ask the user for the file name. The function should check the file state of the input file and return a value indicating success or failure. Main should check the returned value and if the file...

  • C++ Write a program that prompts the user to enter integers or a sentinel to stop....

    C++ Write a program that prompts the user to enter integers or a sentinel to stop. The program prints the highest and lowest numbers entered, the sum and the average. DO NOT USE ARRAYS, only variables and loops. Write a program the prompts the user to input a positive integer. Keep asking the user until a positive integer is entered. Once a positive integer is entered, print a message if the integer is prime or not. (NOTE: A number is...

  • In Java Write a program that reads an arbitrary number of 25 integers that are positive...

    In Java Write a program that reads an arbitrary number of 25 integers that are positive and even. The program will ask the user to re-enter an integer if the user inputs a number that is odd or negative or zero. The inputted integers must then be stored in a two dimensional array of size 5 x 5. Please create 3 methods: 1. Write a method public static int sum2DArray( int [1] inputArray ) The method sums up all elements...

  • Write a program that reads a set of integers and then finds and prints the sum...

    Write a program that reads a set of integers and then finds and prints the sum of the even and odd integers

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
ADVERTISEMENT