1. Procedure
import java.util.ArrayList;
import java.util.List;
public class PrimeNumbers {
public static List<Integer> findPrimeNumbers(int limit) {
List<Integer> primeNumbers = new ArrayList<>();
for (int num = 2; num <= limit; num++) {
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
primeNumbers.add(num);
}
}
return primeNumbers;
}
public static void main(String[] args) {
int limit = 100;
List<Integer> primeNumbers = findPrimeNumbers(limit);
System.out.println(primeNumbers);
}
}
Running this code will output prime numbers within 100.
This code uses two nested loops to iterate through all numbers from 2 to a specified limit. For each number, it will check if it is divisible by a number less than or equal to its square root, and if so, mark it as non-prime. If a number is not divisible by any number, it is added to the list of prime numbers. Finally, print out the list of prime numbers.
2. Example
If you do not have a Java running environment, you can use an online Java editor.
https://c.runoob.com/compile/10/
Enter the program on the left, click Run, and output the results on the right.