How to do it without using flag variable? using the flag is a bit confusing

why is it not running when i am simply using loop if else loop inside the while loop.

hey @adityaraj9011_7f58157f1a14361d Send your code.

import java.util.Scanner;

public class PrimeNumberDemo {

public static void main(String[] args) {
	
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();

            int divisor = 2;
            while(divisor <= n - 1) { 
                             
                       if(n % divisor == 0) {
                      
                                    System.out.println("Not Prime");
                             }divisor = divisor + 1;
                                else {
                                        System.out.println("Prime")
                                         }
                      }
            }
       }

@adityaraj9011_7f58157f1a14361d You cannot add any line of code between if else so remove divisor = divisor + 1; after else statement like :

public static void main(String[] args) {
	
	Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();

            int divisor = 2;
            while(divisor <= n - 1) { 
                             
                       if(n % divisor == 0) {
                      
                                    System.out.println("Not Prime");
                             }
                                else {
                                        System.out.println("Prime")
                                         }
divisor = divisor + 1;
                      }
            }
       }

This Question. Even after using the solution provide by you i am not able to get the desired output.

hey @adityaraj9011_7f58157f1a14361d Refer to this code lin(I have made some changes).
https://ide.geeksforgeeks.org/faad88f2-0734-4f7c-9913-f5d18af41243

hey @adityaraj9011_7f58157f1a14361d I have replied you on Chat section.

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.