Prime number Question

import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int n = scn.nextInt();
boolean flag = true;
int div = 2;
while (div <= n-1){
if (n%div == 0){
flag = false;
}
else flag = true;
div = div +1;
}
if (flag == true) {
System.out.println(“Prime”);
}
else System.out.println(“Non prime”);
}
}

Why the above question is giving me wrong output? I only included ELSE from my side. But it should have given the same output. Explain please

@rishujha2598_131feaf06c9388b8 Your code is givign TLE(Time Limit Exceeded) because you have to use SOE approach to solve this question. This will be taught later in the course.

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.