JAVA输出100以内的所有素数
时间: 2023-09-03 20:11:55 浏览: 147
以下是Java代码,可以输出100以内的所有素数:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int n = 100;
// loop through all numbers from 2 to n
for (int i = 2; i <= n; i++) {
boolean isPrime = true;
// check if i is a prime number
for (int j = 2; j <= Math.sqrt(i); j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
// if i is a prime number, print it
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
```
运行该程序,将输出以下内容:
```
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
```
阅读全文
相关推荐















