For loop output primes Explore "Java"

Source: Internet
Author: User

One, Judge 953 is not a prime number (prime number).

Code:

1  /**2 Judge 953 is not a prime number (prime number)3 Analysis:4 primes are integers in a natural number greater than 1, which cannot be divisible by other natural numbers except 1 and the integer itself. 5 Assuming that 953 is a prime number, then:6 953% 2!== 07 953% 3!== 08 953% 4!== 09 ...Ten 953% 952!== 0 One The inverse is that 953 is not a prime number, as long as 953 is divisible by any integer between 2~952.  A  - The divisor is incremented from 2 to 952, followed by integers, and is represented by A for loop: - for (int i=2;i<953;i++) { the Judging whether 953 can be I integer - if it can be divisible, then 953 is not prime, end the loop, jump out of the output "953 is not prime" - if it cannot be divisible, then 953 is the prime, ending the loop, jumping out of the output "953 is prime" -             } +  */ -   +   Public classIsPrime A { at      Public Static voidMain (string[] args) { -  -         intnum = 953; -         BooleanIsPrime =true;//mark number num By default is prime -          for(inti=2;i<num;i++) { -             if(num%i==0) {//num can be 2~num-1 between the natural number of integers inIsPrime =false;//Mark number num as non-prime -                  Break;//End Loop to             } +         } -  theSystem.out.println (isprime?num+ "is prime": num+ "is not prime");  *  $     }Panax Notoginseng}

The Code results show:

Ii. List of all primes between 1~100

Code:

1 /**2 List all primes between 1~1003 Analysis:4 The code snippet above indicates whether the natural number of Num 953 is a prime,5 assuming that NUM is 937, it is judged that 937 is not a prime number,6 assuming that num is 933, it is judged that 933 is not a prime number,7 8 Therefore, NUM is set to a variable that is incremented from 1~100 with a for loop,9 Place the code snippet of the door into the for loop and print it as a prime number. Ten  One for (int num=2;num<=100;num++) { A  - code snippet to determine if Num is a prime number -  the         } -  - */ -  +  Public classIsPrime - { +      Public Static voidMain (string[] args) { A  at          for(intnum=2;num<=100;num++) { -             BooleanIsPrime =true;//mark number num By default is prime -              for(inti=2;i<num;i++) { -                 if(num%i==0) { -IsPrime =false; -                      Break; in                 } -             } to             if(isprime) { +System.out.print (num+ ""); -             } the         } *     } $}

Description of code Explanation:

The Code results show:

List all primes between 1~100, and display 5 numbers per line.

Code:

1 /**2 lists all the primes between 1~100, and displays 5 numbers per row. 3 Analysis:4 you can get all the prime numbers between 1~100 from the previous example code. 5 To show 5 numbers per row, you know that every time a prime number is drawn, you know that it is the first occurrence, and then you accumulate6 when the number is accumulated to 5 o'clock, the line is changed. 7 8 Therefore, a tag is required to record the number of times the output of each prime. 9 Ten */ One  A  Public classIsPrime - { -      Public Static voidMain (string[] args) { the          -         intCount = 0;//A counter that is used to record the number of times each prime is printed.  -          for(intnum=2;num<=100;num++) { -             BooleanIsPrime =true;//mark number num By default is prime +              for(inti=2;i<num;i++) { -                 if(num%i==0) { +IsPrime =false; A                      Break; at                 } -             } -             if(isprime) { -System.out.print (num+ ""); -count++;//record once for each prime number printed -                 if(count%5==0) {//each counter is recorded 5 times, and the line is changed once.  in System.out.println (); -                 } to             } +         } -     } the}

Code distortion: If the counter is implemented every 5 times, then the 0 can also achieve the corresponding count effect. Replace the above 25 lines of ~31 line code with the following code:

1 if (isprime) {2 System.out.print (num+ ""); 3 // record once for each prime number printed 4 if // The counter accumulates to the number 5 o'clock, and the line is changed one time. 5 System.out.println (); 6 // counter is placed 0 7     }8 }         

The Code results show:

Iv. Output 9*9 Multiplication table

Code:

1 /**2 output display 9*9 multiplication table3 1*1=14 1*2=2 2*2=25 1*3=3 2*3=6 3*3=96 1*4=4 2*4=8 3*4=12 4*4=167 ...8 1*9=9 2*9=18 ... and so on.  9*9=819 Ten Analysis: Follow the rules of each row and column One -The table is 9 rows in total, A -Several equations are required for each output of the first few lines -  - identity format is i*j=c the • Each line I increments from 1 to J, indicating that each J-row output can output a total of I equation - so using a for loop is represented as: - for (int i=1;i<=j;j++) { - ... +                 } - • Indicates line J + for (int j=1;i<=9;j++) { A Output Line//The loop body executes once, and then wraps once.  at                 } - so the first for loop body needs to be within the second for loop body.  - */ -  -  Public classTest03 - { in      Public Static voidMain (string[] args) { -          for(intj=1;j<=9;j++) { to              for(inti=1;i<=j;i++){ +System.out.print (i+ "*" +j+ "=" +i*j+ "");//i from 1~j cycle show, line J need to display 1~j identities -             } theSystem.out.println ();//each of the above I loop body performs an end, that is, the line is displayed, the line is changed once *         } $     }Panax Notoginseng}

The Code results show:

For loop output primes Explore "Java"

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.