判断101-200之间有多少个素数,并输出所有素数。素数为一个大于1的自然数,除了1和它本身外,不能被其他自然数整除。(素数也称质数)

package com.example.springboot.caipiao;

public class ZhiShu {
    public static void main(String[] args) {
        for (int i=101;i<200;i+=2)
        {
            boolean a=false;
            //Math.sqrt(i) 对i开根
            for (int j=2;j<Math.sqrt(i);j++)
            {
                if(i%j==0)
                {
                    a=false;
                    break;
                }
                else
                {
                    a=true;
                }
            }
              if (a){
                  System.out.println("质数:"+i+"\t");
              }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_41799291/article/details/88124830