How many prime numbers between Java implementations 1~100 and the output of all primes

Source: Internet
Author: User


/*

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.