how many numbers do we have to get from scanner
Question is not clear
You have to take input from user and then print the number until you face comulative sum negative
Hey @faizahmed2696
you do not know. How many numbers to take from scanner
In this scenario we use infinite loop for the scanner
if comulative sum is negative, then break the loop
then in sample input why did we take the one extra input of 49? Because the cumulative sum was already becomes negative when we had taken the input of -100. for example 1+2=3, 3+88=91, 91-100=-9
But your loop will break before 49.
then we should have to stop the loop after taking the input of -100
yes if cumulative sum is -ve . then break the loop
the cumulative sum is -ve after taking the input of -100. If i break the loop then how can we take the input of 49 after that?
This is the sample input:
1
2
88
-100
49
@faizahmed2696
dry run this logic
while(true){ // while(true) se infinite loop chalega
int num = sc.nextInt();
sum += num;
if (sum>=0){
System.out.println(num);
}
else{
return;
}
}
I dont get it how this code is accepted ! isme to hum hand to hand output print kr rhe but we want to print all the outputs after all the input numbers
can u plz eplain more because when I run on intelliJ after the giving the input of 1 it prints 1 and then after giving the input of 2 it prints 2 after that giving the i/p of 88 it prints 88 then after giving the input of -100 it stops the execution of program.
like this:
1
1
2
2
88
88
-100
uploaded the screeshot of the output:

but in the question i/p and o/p formats are different.
If it is not the way the question wanted then how it got accepted
1+2+88=91
mai ab -100 input diya.
sum is -ve.
My code entered in else part .
and terminate here
f (sum>=0){
System.out.println(num);
}
else{
return;
There is a separate input output file on the admin panel.
while(true){ // while(true) se infinite loop chalega
int num = sc.nextInt();
sum += num;
if (sum>=0){
System.out.println(num);
}
else{
return;
}//its correct is i/p and o/p format