How to generate prime numbers between 1 and given number using for loop - Java Data Structure

Java examples for Data Structure:Algorithm

Description

How to generate prime numbers between 1 and given number using for loop

Demo Code

public class Main {

  public static void main(String[] args) {
    int limit = 100;

    System.out.println("Prime numbers between 1 and " + limit);

    for (int i = 1; i < 100; i++) {
      boolean isPrime = true;
      for (int j = 2; j < i; j++) {
        if (i % j == 0) {
          isPrime = false;//from  w ww  . j ava2 s .co m
          break;
        }
      }
      if (isPrime){
         System.out.print(i + " ");
      }      
    }
  }
}

Result


Related Tutorials