用java找到2到100之间的所有质数
时间: 2024-03-08 18:49:34 浏览: 86
好的,以下是用Java实现的代码:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int lowerBound = 2;
int upperBound = 100;
System.out.println("Prime numbers between " + lowerBound + " and " + upperBound + " are: ");
for (int i = lowerBound; i <= upperBound; i++) {
if (isPrime(i)) {
System.out.print(i + " ");
}
}
}
public static boolean isPrime(int number) {
if (number <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
```
这个程序会输出2到100之间的所有质数,结果如下所示:
```
Prime numbers between 2 and 100 are:
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
```
阅读全文