Two test cases are not matching

#include
using namespace std;
int main() {
int a[10],i;
for(i=0;i<10;i++)
cin>>a[i];
for(i=0;i<10;i++)
{
if(a[i]>0)
{
cout<<a[i]<<endl;
}
else
break;
}

}

Hello @supratik260699

You need to stop when the sum of all the elements uptil i goes zero and NOT when any negative element is found.
Also in your code when you input elements like this
for(i=0;i<10;i++)
cin>>a[i];

This is wrong because you don’t know how many elements you need to take as input, it’s not predefined

DO IT LIKE THIS

#include<iostream>
using namespace std;
int main(){
int sum = 0;
int a;
while(true) {
cin >> a;
sum = sum + a;
if(sum >= 0) cout << a << endl;
else break;
}

If you still have any doubt, just reply to this thread

1 Like