Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

how to print only prime numbers in java

public class PrimeNumbers {

 public static void main(String[] args) {

  int num = 20, count;

  for (int i = 1; i <= num; i++) {
   count = 0;
   for (int j = 2; j <= i / 2; j++) {
    if (i % j == 0) {
     count++;
     break;
    }
   }

   if (count == 0) {
    System.out.println(i+"is prime number");
   }

  }
 }
}
Program Output:
Source by www.w3schools.in #
 
PREVIOUS NEXT
Tagged: #print #prime #numbers #java
ADD COMMENT
Topic
Name
7+7 =