File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed
Section7ConditionalStatements/Loops/src Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 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 ("\n Let'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+ }
You can’t perform that action at this time.
0 commit comments