码迷,mamicode.com
首页 > 编程语言 > 详细

Java入门:基础算法之检查素数

时间:2016-04-05 22:37:10      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

程序提示用户输入一个数,然后检查所输入的数是否是素数。

import java.util.Scanner;
class PrimeCheck
{
   public static void main(String args[])
   {        
    int temp;
    boolean isPrime=true;
    Scanner scan= new Scanner(System.in);
    System.out.println("Enter a number for check:");
    //capture the input in an integer
    int num=scan.nextInt();
    for(int i=2;i<=num/2;i++)
    {
           temp=num%i;
       if(temp==0)
       {
          isPrime=false;
          break;
       }
    }
    //If isPrime is true then the number is prime else not
    if(isPrime)
       System.out.println(num + " is Prime Number");
    else
       System.out.println(num + " is not Prime Number");
   }
}

输出:

Enter a number for check:
19
19 is Prime Number

 

Java入门:基础算法之检查素数

标签:

原文地址:http://www.cnblogs.com/bayes/p/5356942.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!