Print prime number - Java program

Q. Write a program to print the prime number in the given range.

Answer:

Prime numbers are those number which divisible by 1 or itself.
In this example, we calculate the prime number between the given range.

PrimeNumber.java

class PrimeNumber
{
        public static void main(String[] args)
        {
                int number1 = 4;
                int number2 = 30;
                int flag = 0;
                System.out.println("Prime numbers  between "+number1+" and "+number2+" are= ");
                for (int i=number1; i<=number2; ++i)
                {
                        for (int j=2; j<=number1/2 ;++j )
                        {
                                if(i % j==0)
                                {
                                        flag=1;
                                        break;
                                }
                                else
                                flag = 0;
                        }
                        if (flag==0)
                        {
                                System.out.println(i);
                        }
                }
        }
}


Output:

prime number