Design a program in Java that displays all of the prime numbers from 1 through 100. The program should have a loop that calls the isPrime() function.
Use CONSTANTS to set the maximum number in the table to 100 and the number of entries per row to 5.
Table of prime numbers from 1 to 100
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
public class PrimeNumbersList { public static boolean isPrime(long n){ for(int i = 2;i<n;i++){ if(n%i == 0) { return false; } } return true; } public static void main(String[] args) { System.out.println("Table of prime numbers from 1 to 100"); int k = 1; for(int i = 1;i<=100;i++){ if(isPrime(i)) { System.out.print(i + " "); k++; } if(k%6==0){ System.out.println(); k = 1; } } } }
Get Answers For Free
Most questions answered within 1 hours.