You are here: Home / Topics / Program to check Prime Number using for loop in java

Program to check Prime Number using for loop in java

Filed under: Java on 2023-08-18 06:50:30

// Program to Check the Given Number is PRIME or NOT.

class  PrimeTest
{
public static void main( String args[ ] )
{
 int i, n;
 boolean  flag = true;
 
 n = 29;  

 for( i=2 ; i<=n/2 ; i++ )
 {
  if( n % i == 0 )
  {
   flag = false;
   break;
  }
 }
 
 if( flag == true )
  System.out.print( "\n Number " + n + " is a PRIME Number." );
 else
  System.out.print( "\n Number " + n + " is NOT a PRIME Number." );
}
}


Output:

Number 29 is a PRIME Number.

About Author:
Y
Yatendra Sir     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.