程序分析:

        1.素数的定义,除了1和它本身以外不再有其他的因数的数叫素数。

        2.根据素数的定义,只需要判断2~该数的平方根之间有没有数是数本身的因数就可以判读该数是否是素数,如果没有为素数,否则为合数。

程序代码:

package cn.wdl.demo;

public class Case13 {

/*
* 函数功能:判断一个数是否是素数,如果是返回true,否则返回false。
* 函数参数:待判断数。
* 函数返回值:true:素数,false:合数
* */
public boolean isPrime(int num) {
boolean flag=true;
for(int i=2;i<=Math.sqrt(num);i++) {
if(num%i==0) {
flag = false;
break;
}
}
return flag;
}

public static void main(String[] args) {
Case13 case13 = new Case13();
int count=0;
for(int i=101;i<=200;i++) {
if(case13.isPrime(i)) {
count++;
System.out.print(i+"\t");
if(count%5==0) {
System.out.print("\n");
}
}
}
}

}

输出结果:

101 103 107 109 113

127 131 137 139 149

151 157 163 167 173

179 181 191 193 197

199