Need guidance in the following question

  1. https://online.codingblocks.com/app/player/246132/content/235743/4693/code-challenge

Hi @sbhardwaj1be21_2a991b329433c059
You may be Wondering where is the N(No of inputs specified)?

Till when we need to take input??

Ans to the question is present in the question itself, as it said print the numbers till u get positive cumulative sum, so u need to take input till u got the positive cumulative irrespective of the number inputs.

Algo:

Put a loop till the END OF INPUT.
Add the new number to the previous sum.
If the sum is positive print the current number.
Otherwise, break the loop.
refer this code–>


#include <bits/stdc++.h>
using namespace std;

int main() {
    int sum=0;
    int n;

    while(1){
        cin >> n;
        sum += n;
        if(sum<0)
            break;
        cout << n << '\n';
    }

    return 0;
}
1 Like