(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