Skip to content

Commit 32af786

Browse files
committed
feat: Identify and print prime numbers from 2 to 20 using nested loops
🔍 Logic: - Outer loop increments through numbers 2 to 20. - Inner loop checks divisibility from 2 to current number. - If `divisor == currentNumber`, it means no smaller divisor was found → prime. - If divisible earlier, print "NOT a prime" and exit inner loop early. 🛠️ Note: `break` should be used after detecting a non-prime to skip unnecessary checks. Signed-off-by: Somesh diwan <[email protected]>
1 parent 39fb052 commit 32af786

File tree

1 file changed

+4
-7
lines changed

1 file changed

+4
-7
lines changed

Section7ConditionalStatements/Loops/src/FindingPrimes.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
public class FindingPrimes
2-
{
3-
public static void main(String[] args)
4-
{
1+
public class FindingPrimes {
2+
public static void main(String[] args) {
53
int currentNumber = 2; //Number analyzed
64
int maxNumber = 20; //Maximum value to be checked
75

86
//Main loop that goes from 2 to 20
9-
while(currentNumber <= maxNumber)
10-
{
7+
while(currentNumber <= maxNumber) {
118
//Loop that checks all integers from 2 to the current number (starts with 2, ends with 20)
129
for (int divisor = 2; divisor <= currentNumber; divisor++) {
1310
//We know that the number is prime if it already checked all previous divisors
@@ -25,4 +22,4 @@ public static void main(String[] args)
2522
currentNumber++;
2623
}
2724
}
28-
}
25+
}

0 commit comments

Comments
 (0)