Skip to content

Commit 1052245

Browse files
committed
Break and Continue Practice Added.
Signed-off-by: Someshdiwan <[email protected]>
1 parent 78b0787 commit 1052245

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
public class BreakAndContinue
2+
{
3+
public static void main(String[] args)
4+
{
5+
int number = 143;
6+
System.out.println("Let's check if " + number + " is a prime number.");
7+
for (int divisor = 2; divisor < number; divisor++)
8+
{
9+
int remainder = number % divisor; //The modulo operator gives the remainder of the division involved.
10+
if (remainder == 0)
11+
{
12+
System.out.println("The number is not prime since it is divisible by " + divisor + ".");
13+
break; //Exit the for loop
14+
}
15+
if (divisor == number-1)
16+
System.out.println("The number is prime!");
17+
}
18+
19+
System.out.println("\nLet's check how many factors it has.");
20+
int factor = 2;
21+
while (factor < number)
22+
{
23+
int remainder = number % factor; //The modulo operator gives the remainder of the division involved.
24+
factor++;
25+
if (remainder != 0)
26+
{
27+
continue; //Proceed to the next iteration of the loop, ignoring the rest of this iteration.
28+
}
29+
System.out.println(factor-1 + " is a factor of 143.");
30+
}
31+
32+
}
33+
}

0 commit comments

Comments
 (0)