Answer to Question #282205 in C for sunny

Question #282205

I always want to look at the positive side of things, so I decide to seriously look at positive numbers, too!


Will you code along with me?


Instructions:

  1. Using the do…while() loop, continuously scan for integers, but add up only all the positive integers and store the total in one variable.
  2. The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.

Input


1. A series of integers

Output

The first multiple lines will contain message prompts to input the integers.

The last line contains the total of all positive integers inputted.

radio_button_unchecked


Test Case 1

expand_less

Your Output

No Output

Expected Output

Enter n: 2
Enter n: 3
Enter n: 4
Enter n: -1
Enter n: -5
Enter n: 1
Enter n: 0
Total of all positives = 10

radio_button_unchecked

Test Case 2

expand_less

Your Output

No Output

Expected Output

Enter n: 5
Enter n: 5
Enter n: 5
Enter n: 5
Enter n: 5
Enter n: 0
Total of all positives = 25




1
Expert's answer
2021-12-24T01:20:44-0500
#include<bits/stdc++.h>
using namespace std;


int main()
{
    int n,sum = 0;
    
    do{
        cout<<"Enter a Number: ";
        cin>>n;
        
        if(n > 0)
        {
            sum += n;
        }
    }
    while (n != 0);
    
    
    cout<<"Total of all positives: "<<sum;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS