用java编写100-150之间的素数
时间: 2024-02-23 16:00:38 浏览: 105
java代码-46 赖赵强 1-100里面的素数
以下是Java代码,用于找出100到150之间的所有素数:
```
public class PrimeNumbers {
public static void main(String[] args) {
int lowerLimit = 100;
int upperLimit = 150;
System.out.println("Prime numbers between " + lowerLimit + " and " + upperLimit + " are:");
for (int i = lowerLimit; i <= upperLimit; 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;
}
}
```
输出结果:
```
Prime numbers between 100 and 150 are:
101 103 107 109 113 127 131 137 139 149
```
阅读全文