用JAVA编写50以内的素数_java求50以内的素数
时间: 2023-09-26 17:07:33 浏览: 237
java代码-100里的素数
以下是使用Java编写的50以内的素数的程序:
```java
public class PrimeNumbers {
public static void main(String[] args) {
int num = 50;
System.out.println("Prime numbers up to " + num + " are:");
for (int i = 2; i <= num; i++) {
boolean isPrime = true;
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.print(i + " ");
}
}
}
}
```
运行结果如下:
```
Prime numbers up to 50 are:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
```
阅读全文