1 打印出100以内的质数,一行显示5个,
(质数是只能被1跟自身整除的数,1不是质数)
public class Test4
{public static void main(String args[])
{
boolean x = true;
int k = 0;
for(int i = 2; i <= 100; i++)
{
for(int j = 2; j < i; j++)
{
if(i % j == 0)
{
x = false;
break;
}
}
if (x == true)
{
System.out.print(i+" ");
k++;
if (k == 5)
{
System.out.println(" ");
k = 0;
}
}
}
}
}
///
public class Test4 {
public static void main(String[] args) {
int k=0;
for (int i = 1; i < 101; i++) {
for (int j = 2; j <= i; j++) {
if (i % j == 0 && i != j) {
break;
} else if (i == j) {
System.out.print(i+" ");
k++;
if(k==5){
System.out.println("");
k=0;
}
}
}
}
}
}
2 /*所谓素数(又叫质数)是指只能被1和它本身整除的数字,1除外。
* 输入一个正整数,判断是否为素数。
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个正整数");
int num = sc.nextInt();
boolean x = true;
for (int i = 2; i <=num; i++) {
if (num % i == 0 && num != i) {
System.out.println("不是素数");
break;
}
else if (num == i) {
System.out.println("该数是素数");
}
}
}
}
///
import java.util.Scanner;
public class Test3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数:");
int num = sc.nextInt();
boolean x = true;// 定义一个布尔变量
for (int i = 2; i < num; i++) {
if (num % i == 0) { // 将该数与比它小的所有整数相除
System.out.println(num + "不是素数");
x = false;
break;
}
}
if (x) {
System.out.println(num + "是素数");
}
}
}