Java求100以内的素数
时间: 2023-09-12 17:12:40 浏览: 139
JAVA找出100以内的素数
以下是Java代码,用于找到100以内的素数:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int num = 100;
System.out.println("Prime numbers between 1 and " + num);
for (int i = 2; i <= num; i++) {
boolean isPrime = true;
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
```
输出结果为:
```
Prime numbers between 1 and 100
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
```
阅读全文