Java Prime Number Program

 

Program to find whether a given number is prime or not.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
/**
* This program is used to find that given number is prime or not.
* @author W3schools360
*/
public class PrimeNumber {
/**
* This method is used to find that given number is prime or not.
* @param num
*/
static void primeNumber(int num){
int count = 0;
//0 and 1 are not prime numbers.
if(num == 0 || num == 1){
System.out.println(num + " is not prime.");
}else{
for(int i = 1; i <= num/2; i++){
if(num
count++;
}
}
if(count > 1){
System.out.println(num + " is not prime.");
}else{
System.out.println(num + " is prime.");
}
}
}
public static void main(String args[]){
//method call
primeNumber(37);
}
}
/** * This program is used to find that given number is prime or not. * @author W3schools360 */ public class PrimeNumber { /** * This method is used to find that given number is prime or not. * @param num */ static void primeNumber(int num){ int count = 0; //0 and 1 are not prime numbers. if(num == 0 || num == 1){ System.out.println(num + " is not prime."); }else{ for(int i = 1; i <= num/2; i++){ if(num count++; } } if(count > 1){ System.out.println(num + " is not prime."); }else{ System.out.println(num + " is prime."); } } } public static void main(String args[]){ //method call primeNumber(37); } }
/**
 * This program is used to find that given number is prime or not.
 * @author W3schools360
 */

public class PrimeNumber {
      /**
       * This method is used to find that given number is prime or not.
       * @param num
       */
      static void primeNumber(int num){
            int count = 0;
            //0 and 1 are not prime numbers.
            if(num == 0 || num == 1){
                  System.out.println(num + " is not prime.");
            }else{
                  for(int i = 1; i <= num/2; i++){ 
                    if(num 
                       count++; 
                    } 
                  } 
                  if(count > 1){
                      System.out.println(num + " is not prime.");
                  }else{
                      System.out.println(num + " is prime.");
                  }
            }
      }    

      public static void main(String args[]){
            //method call
            primeNumber(37);
      }
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
37 is prime.
37 is prime.
37 is prime.