|
Line Number |
Algorithm Step (Flowchart) |
Description |
|
1 |
- |
Imports the Scanner class for input handling. |
|
2 |
1 (START) |
Class declaration: public class Sieve. |
|
3 |
- |
Method declaration: public static void main(String[] args). |
|
4 |
- |
Declaration of integer variables n, i, j. |
|
5 |
- |
Initialization of the Scanner object named key. |
|
6 |
- |
Writes the prompt "n = " to the console. |
|
7 |
2 (Read n) |
Reads the integer value for n from the user input. |
|
8 |
3 (n < 2), 4 (Write "bad n") |
Checks the condition n < 2 and writes the error message if true. |
|
9 |
- |
Opens the else block (executed if n > 2). |
|
10 |
- |
Declaration and initialization of the boolean array del (size n+1). |
|
11 |
5 (i := 2), 6 (del[i] := false, i := i+1), 7 (i < n) |
Initializes the del array elements from index 2 up to n to false. |
|
12 |
8 (i := 2) - Initial value is set here |
Initializes the control variable i to 1 before the main loop. |
|
13 |
- |
Opens the do loop block. |
|
14 |
13 (i := i+1), 18 (i := i+1) |
Increments i by 1. |
|
15 |
9 (del[i] == false) |
Checks if the number i is not marked as deleted (if(!del[i])). |
|
16 |
10 (j := i * i) |
Initializes j to the square of i (i * i). |
|
17 |
11 (j < n) |
Condition for the inner while loop. |
|
18 |
12 (del[j] = true) |
Marks the current multiple j as deleted/not prime. |
|
19 |
12 (j := j+i) |
Moves to the next multiple of i. |
|
20 |
- |
Closing brace for the while loop. |
|
21 |
- |
Closing brace for the if(!del[i]) block. |
|
22 |
14 (i^2 < n) |
Condition for the outer do-while loop. |
|
23 |
15 (i := 2), 16 (del[i] == false), 17 (Write: i), 19 (i > n) |
for loop: Iterates from i=2 to n to check for and print undeleted (prime) numbers. |
|
24 |
- |
Closing brace for the else block. |
|
25 |
- |
Closing brace for the main method. |
|
26 |
20 (STOP) |
Closing brace for the class Sieve. |
