/*
2. Determine the number of primes between 1-100 and the output of all primes.
(The function of determining whether a number is prime is extracted into a method, which can be called directly in the loop)
*/
public class primenumber{
public static void Main (String[]args)
{
int count = 0;
Generates 1~100 between A for loop
for (int i=1;i<=100;i++)
{
if (IsPrime (i)) {
System.out.println (i);
count++;
}
}
SYSTEM.OUT.PRINTLN ("Total number of primes between 1~100" +count);
}
Determine if a number is prime
public static Boolean isprime (int num)
{
Boolean flag=true;
Count the number of primes
int count = 0;
int i = 2;
if (num==1)
{
Flag=false;
}
Else
{
for (I=2;I<=MATH.SQRT (num); i++)
{
if (num%i==0)
{
Flag=false;
return flag;
}
}
}
return flag;
}
}
This article is from the "11890208" blog, please be sure to keep this source http://11900208.blog.51cto.com/11890208/1864766
How many prime numbers between Java implementations 1~100 and the output of all primes