Cumulative sum till -ve sum

Sir, what are the problem with my code .

Hey @sb8031151_af528e7b895c738c The problem with your code is that you are not given how many numbers you have to take input . You have taken first input number as ‘n’ whereas in the question it is not given that first input number is size of inputs. NO. NO. NO. You have to do this question without array. Just take input and if sum becomes negative stop taking input otherwise again take input .
Like this :

  1. Put a loop till the END OF INPUT.
  2. Add the new number to the previous sum.
  3. If the sum is positive print the current number.
  4. Otherwise, break the loop.

Java Code

 public static void takeInput(){

        Scanner scn = new Scanner(System.in);
        int sum = 0;

        while(sum >= 0){

            int n = scn.nextInt();
            sum += n;

            if(sum < 0) break;
            System.out.println(n);
        }
    }

Now I have solved it. sir
thanks sir.

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.