Check number prime or not without using boolean flag

Scanner s = new Scanner(System.in);
int n = s.nextInt();

	int div = 2;

	while (div <= (n - 1)) {

		if (n % div == 0) {
			System.out.println("not prime");
		} else {

			System.out.println("prime");
		}
		div = div + 1;
	}

Hi nipun,here you dont have to print in every case,if(n%div==0) then print not prime and return from here and outside the loop write condition that if your div==n then print prime.we have done this as even if once the number gets divisible by any of the number(div)then it is not prime else it will be prime.

Hi Nipun,
As you are not responding to this thread, I am marking your doubt as Resolved for now. Re-open it if required.

Please mark your doubts as resolved in your course’s “ Ask Doubt ” section, when your doubt is resolved.