Skip to content

Commit e8c4066

Browse files
committed
feat: Count numbers divisible by both 2 and 3 from 1 to 10 using while loop
✅ Logic: - Iterates from 1 to 10 - Checks divisibility by both 2 and 3 using modulus - Increments counter if condition is true and prints results. Signed-off-by: Somesh diwan <[email protected]>
1 parent 9663cec commit e8c4066

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
public class WhileUsingModulus
2-
{
3-
public static void main(String[] args)
4-
{
1+
public class WhileUsingModulus {
2+
public static void main(String[] args) {
53
int num = 1;
64
int count = 0;
7-
//while number is less than, or equal to, 10
8-
while (num <= 10)
9-
{
10-
//if the number is divisible by 2 (i.e. even) AND divisible by 3, then...
11-
if (num % 2 == 0 && num % 3 == 0)
12-
{
5+
6+
System.out.println("Checking numbers from 1 to 10:");
7+
8+
while (num <= 10) {
9+
System.out.print("Number " + num + " is ");
10+
11+
if (num % 2 == 0 && num % 3 == 0) {
12+
System.out.println("divisible by both 2 and 3!");
1313
count++;
14+
} else {
15+
System.out.println("not divisible by both 2 and 3");
1416
}
1517
num++;
1618
}
19+
20+
System.out.println("\nTotal numbers divisible by both 2 and 3: " + count);
1721
}
1822
}

0 commit comments

Comments
 (0)