prime numbers java

  1. /*
  2.   Prime Numbers Java Example
  3.   This Prime Numbers Java example shows how to generate prime numbers
  4.   between 1 and given number using for loop.
  5. */
  6. public class GeneratePrimeNumbersExample {
  7. public static void main(String[] args) {
  8. //define limit
  9. int limit = 100;
  10. System.out.println("Prime numbers between 1 and " + limit);
  11. //loop through the numbers one by one
  12. for(int i=1; i < 100; i++){
  13. boolean isPrime = true;
  14. //check to see if the number is prime
  15. for(int j=2; j < i ; j++){
  16. if(i % j == 0){
  17. isPrime = false;
  18. break;
  19. }
  20. }
  21. // print the number
  22. if(isPrime)
  23. System.out.print(i + " ");
  24. }
  25. }
  26. }
  27. /*
  28. Output of Prime Numbers example would be
  29. Prime numbers between 1 and 100
  30. 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
  31. */




0 comments: