1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Brilliant_brown [7]
4 years ago
5

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.
Computers and Technology
1 answer:
Pani-rosa [81]4 years ago
8 0

Answer:

The loop for the given scenario is shown.

while( num >= 0 )

{

       if( num%2 == 0 )

           sum_even = sum_even + num;

       else

           sum_odd = sum_odd + num;

       cout << "Enter a positive number." << endl;

       cin  >>  num;

}

Explanation:

The program uses three variables to hold the user input, sum of even numbers and sum of odd numbers.

int sum_even, sum_odd, num;

The sum variables are initialized.

sum_even = 0;

sum_odd = 0;

Only positive input from the user needs to be considered as per the question, hence, while loop is used.

This loop will execute only if the user inputs valid number. Upon entering the negative number which is less than 0, the loop terminates.

Following this, the sum of even numbers is displayed followed by the sum of negative numbers.

cout << sum_even << " " << sum_odd << endl;

The c++ program for the given problem statement is as follows.

#include <iostream>

using namespace std;  

 

int main()  

{  

   

   // variables declared for sum and input

   int sum_even, sum_odd, num;

   

   // sum initialized to 0

   sum_even = 0;

   sum_odd = 0;

   

   cout << "This program calculates the sum of even and odd numbers which are positive. This program will end on invalid input." << endl;

   cout << "Enter a positive number." << endl;

   cin >> num;

   

   // loop will terminate if the user inputs negative number

   while( num >= 0 )

   {

       // for even numbers, input is added to sum_even else sum_odd

       if( num%2 == 0 )

           sum_even = sum_even + num;

       else

           sum_odd = sum_odd + num;

           

       cout << "Enter a positive number." << endl;

       cin >> num;

   }

   

   cout << sum_even << " " << sum_odd << endl;

   

   return 0;

   

}

OUTPUT

This program calculates the sum of even and odd numbers which are positive. This program will end on invalid input.              

Enter a positive number.                                                                                                          

23                                                                                                                                

Enter a positive number.                                                                                                          

34                                                                                                                                

Enter a positive number.                                                                                                          

45                                                                                                                                

Enter a positive number.                                                                                                          

67                                                                                                                                

Enter a positive number.                                                                                                          

-69                                                                                                                              

34 135  

You might be interested in
What is Processor to Memory Mismatch problem?
Gala2k [10]
Direct-mapped cache;
fully associative cache;
N-way-set-associative cache.
That is all I could find sorry I can’t help
8 0
3 years ago
Choose all that apply.
stiks02 [169]

Answer:

choose all that apply

Explanation:

7 0
3 years ago
Let G = (V, E) be an undirected graph. Design algorithms for the following (in each
Degger [83]

Answer:

i think its b if not sorry

Explanation:

3 0
3 years ago
Write a method reverse that takes an array as an argument and returns a new array with the elements in reversed order. Do not mo
mr Goodwill [35]

Answer:

public class ArrayUtils

{

//function to reverse the elements in given array

public static void reverse(String words[])

{

//find the length of the array

int n = words.length;

//iterate over the array up to the half

for(int i = 0;i < (int)(n/2);i++)

{

//swap the first element with last element and second element with second last element and so on.

String temp;

temp = words[i];

words[i] = words[n - i -1];

words[n - i - 1] = temp;

}

}

public static void main(String args[])

{

//create and array

String words[] = {"Apple", "Grapes", "Oranges", "Mangoes"};

//print the contents of the array

for(int i = 0;i < words.length;i++)

{

System.out.println(words[i]);

}

//call the function to reverse th array

reverse(words);

//print the contents after reversing

System.out.println("After reversing............");

for(int i = 0;i < words.length;i++)

{

System.out.println(words[i]);

}

}

Explanation:

4 0
2 years ago
Online databases allow you access to material you may not be able to find when using popular search engines like Google. Select
Anna007 [38]

Answer:

True

Explanation:

6 0
4 years ago
Other questions:
  • Look at the circuit shown in the figure above. Switch S1 is open as shown, and R1 and R2 each have a value of 100 k. If you conn
    11·1 answer
  • This is not an appropriate business use for a wiki. editing corporate documents getting customer feedback project management pub
    11·1 answer
  • Write a program that reads a stream of integers from a file and writes only the positive numbers to a second file. The user shou
    5·1 answer
  • In general, digital to analog modulation equipment is less expensive than the equipment for encoding digital data into a digital
    8·1 answer
  • What code do I have to use in my php if i don’t want my $_SESSION[“variablex”] to destroy? But i already have a session_destroy(
    6·1 answer
  • A large computer software firm promised a client that it could deliver a new operating system on a tight deadline and put Keith
    10·2 answers
  • if anyone is on a Chromebook, do you ever mean to press backspace, then you accidentally press the power button and think "OH CR
    11·2 answers
  • The different languages that follow specific RULES. These languages use commands
    9·1 answer
  • I am in class 7 should I go with java or python.​
    6·1 answer
  • Type the correct answer in the box. Spell all words correctly.
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!