How is input passed to program here

I am not aware how the input will be passed to my program hence not able to make my program compile.

hey @tamshamohanty25
Many of you are 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:

  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.

The question says that we will be given list of numbers, its not saying we need to take input that is what is confusing me.

This is how my program looks like and its working find in my laptop but when I am trying to compile in codingblock its throwing error.

public static void main(String args[]) {
	Scanner scn = new Scanner(System.in);
        int sum = 0;
        while (true){
            int n = scn.nextInt();
            sum = sum + n;
            if(sum<0){
                break;
            } else {
                System.out.println(n);
            }
        }
    }

Tamsha , where is class :smile:
correct code :
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner scn = new Scanner(System.in);
int sum = 0;
while (true){
int n = scn.nextInt();
sum = sum + n;
if(sum<0){
break;
} else {
System.out.println(n);
}
}
}
}

Hey I just pasted from main(), I do have class in place.

logic is fine , just submit above code

This is the error which I am getting while compiling the code.

Exception in thread β€œmain” java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Main.main(Main.java:7)

Here is my complete code:

import java.util.*;
public class Main {
	public static void main(String args[]) {
	Scanner scn = new Scanner(System.in);
		int sum = 0;
		while (true){
			int n = scn.nextInt();
			sum = sum + n;
			if(sum<0){
				break;
			} else {
				System.out.println(n);
			}
		}
	}
}

@tamshamohanty25 please give custom input

The education is necessary because through this you become an expert and get acknowledge about different important aspects. Also, focus on the IT field, AZ-900 Practice Exam Questions is the most nonpareil platform which assures you 100% guarantee about we have provided demo questions to probe the quality we are offering you success in any IT certification. Just visit us and get the utmost facilities.