zoukankan      html  css  js  c++  java
  • java 判断一个数是否是素数

    //check prime, find whether the input number is prime number or not
    
    import java.util.Scanner;
    
    public class Numbers {
    
        public static void main(String[] args) {
            int num;
            boolean flag = true;
            Scanner ip = new Scanner(System.in);
            System.out.print("Enter a number: ");
            num = ip.nextInt();
            for (int i = 2; i <= num / 2; i++) { // you can also use i <= Math.sqrt(num)
                if (num % i == 0)
                    flag = false;
            }
            if (flag == true)
                System.out.println("It is prime number");
            else
                System.out.println("It is not a prime number");
            ip.close();
        }
    
    }
    
    
    
    OUTPUT:
    Enter a number: 41
    It is prime number
  • 相关阅读:
    进程、线程
    listview
    Jquery小知识点
    Jquery
    ling查询
    linq
    登陆状态显示
    母版页
    js的触发方式
    js
  • 原文地址:https://www.cnblogs.com/sea-stream/p/12098804.html
Copyright © 2011-2022 走看看