The output of prime numbers

Source: Internet
Author: User

(1) Calculate and output the prime number between 3~100.

(2) programming meets the following requirements:

1) According to the 5 outputs of each line;

2) outputs all the primes between any two integers;

3) Enter two integers, outputting the maximum of 10 and the smallest 10 primes between the two integers.

     Public Static voidMain (string[] args) {intCount=1; intB=0; int[] A=New int[100]; Scanner input=NewScanner (system.in); System.out.println ("Enter the starting number:"); intn=Input.nextint (); System.out.println ("Enter the end of the number:"); intm=Input.nextint ();        Input.close (); //determines whether it is a prime number and outputs each of the five rows;         for(inti=n;i<=m;i++){            intJ;  for(j=2;j<i;j++){                if(i%j==0){                     Break; }            }            if(j==i) {                //to save a prime number in an arraya[b]=i; b++; if(count%5==0) {System.out.print (i+" ");                System.out.println (); }Else{System.out.print (i+" "); } Count++;        }} System.out.println (); System.out.println ("Minimum of 10 primes:");  for(intC=0;c<10&&c<count;c++) {System.out.print (A[c]+" ");        } System.out.println (); System.out.println ("Maximum of 10 primes:");  for(intc=count-2;c>count-12;c--) {System.out.print (A[c]+" "); }    }

Idea: use two cycles; the first loop is a loop that takes a prime range, which is entered by the console;

The second loop to determine whether the number is a prime, if the prime is stored in the array;

Since the number of primes is ordered, the number of primes saved to the array is sequential, so the maximum number of 10 primes and the smallest 10 primes can be directly used for the array output.

The output of prime numbers

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.