Simple Input question

No output is coming for this code…
we have to stop suming the numbers if the sum is negative

#include
using namespace std;
int main() {
int n;
while(n>0)
{
cin>>n;
}
int sum=0;

while(1)
{
sum=sum+n;
}
while(sum>0){
cout<<n;
}
return 0;

}

@komalmishra141 print your n if adding that in your sum doesn’t make the sum negative right after adding it to sum. do not calculate sum of all numbers then start printing your n.

NO OUTPUT for this too
#include
int main() {
int n,sum=0;
while(1)
{
std::cin>>n;
}
sum=sum+n;
if(sum<0)
{
std::cout<<n;
}
return 0;
}

@komalmishra141 you have been given multiple numbers and you are just overwriting them in the n. change your logic a little bit.

1 Like

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.