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.

Question
Asked Oct 21, 2019
115 views

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.

check_circle

Expert Answer

Step 1

Following is the loop which prints the sum of all even and odd integers entered by the user and will terminate if the negative integer is entered.

while (n >= 0) {

  // checks if number entered is even or not

  if (n % 2 == 0)

    even += n;//store all even numbers

  else

    odd += n;//store all odd numbers

  //promt user to enter positive number

  cout << "Enter a positive number. ";

  cin >> n;

}

Step 2

C++ program for above scenario is given below:

The program uses three variables n, even, odd to store the number entered by user, sum of all even number and sum of all odd numbers respectively. While loop will execute only if the number entered by the user is positive, if a negative number is entered loop will terminate and will print the sum of all even and odd integers.

Step 3

Program code:

#include <iostream> //header file for input output

 

using namespace std;

int main() {

 

  // declaring variables

 

  int even = 0, odd = 0, n;

  //promt user to enter positive number

  cout << "Enter a positive number. ";

 

  cin >> n;

 

  // loop to check that number entered is non-negative.

 

  while (n >= 0)

  {

 

    // checks if number entered is even or not

    if (n % 2 == 0)

 

      even += n;//store all even integers

 

    else

 

  ...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Describe the process of how to solve the substitution cipher below and solve it: ZFSZB YESUK SQYDJ S...

A: The process is as follows:scan to look for single letter words – they would most likely correspond t...

question_answer

Q: In a file called RemoveVowels.java, write a program that: Asks the user to enter a word of text. Pr...

A: A java program to print the word with all vowels removed is given below;Program:File name: "RemoveVo...

question_answer

Q: First, launch NetBeans and close any previous projects that may be open (at the top menu go to File ...

A: The name of the program is CharCounter.inputEntry is a string type variable and used for storing use...

question_answer

Q: Python 3.7.4: Write a loop that reads positive integers from standard input and that terminates when...

A: Following is the program code that takes input from the user all positive integer and terminates if ...

question_answer

Q: a. Show that the disk moves made in the classic recursive algorithm for the Towers of Hanoi puzzle c...

A: a.Disk moves for Tower of Hanoi puzzle The disk moves that are made in the classic recursive algorit...

question_answer

Q: consider the udp header 12 03 00 0F 00 1E A3 10.What is the total length of the udp

A: The UPD header basically has 4 parts, each part is of 2 bytes. That means following interpretation o...

question_answer

Q: I need help with this problem for C++  If a linear search function is searching for a value that is ...

A: Often called sequential search is linear search. Linear search is a method within an array to check ...

question_answer

Q: I need help with this programming exercise:   Write a C++ console application that allows your user ...

A: Before solving this problem our first duty is to understand what question is asking:Here user wants ...

question_answer

Q: in c++  input file is .txt Write a program that asks for the name of an input file. Then, read all t...

A: In this C++ program, Input is given using file name (xyz.txt) which contains a range of numbers.Outp...