DEV Community

Vigneshwaralingam
Vigneshwaralingam

Posted on

Loop Task while loop for Day 3

Loop Task Day

πŸ” Programming Session 3:

β˜• Let’s Practice Loops with Number Patterns in Java!


πŸ”’ Task 1: Print Numbers from 1 to 5

int i = 1;
while (i <= 5) {
    System.out.print(i + " ");
    i++;
}
Enter fullscreen mode Exit fullscreen mode

Output:

1 2 3 4 5


πŸ”’ Task 2: Print Odd Numbers – 1 3 5 7 9

int i = 1;
while (i <= 9) {
    System.out.print(i + " ");
    i += 2;
}
Enter fullscreen mode Exit fullscreen mode

Output:

1 3 5 7 9


πŸ”’ Task 3: Print Multiples of 3 – 3 6 9 12 15

int i = 3;
while (i <= 15) {
    System.out.print(i + " ");
    i += 3;
}
Enter fullscreen mode Exit fullscreen mode

Output:

3 6 9 12 15


πŸ”’ Task 4: Print Multiples of 4 – 4 8 12 16 20

int i = 4;
while (i <= 20) {
    System.out.print(i + " ");
    i += 4;
}
Enter fullscreen mode Exit fullscreen mode

Output:

4 8 12 16 20


πŸ”’ Task 5: Print Multiples of 5 – 5 10 15 20 25

int i = 5;
while (i <= 25) {
    System.out.print(i + " ");
    i += 5;
}
Enter fullscreen mode Exit fullscreen mode

Output:

5 10 15 20 25


βœ… Summary – What I Learned Today

  • while loop in Java checks a condition and repeats the block.
  • Understanding the start value and step size is key to pattern creation.
  • Loops help reduce repetitive code and make programs efficient.
  • Practice makes patterns easy! πŸ”‚

Top comments (0)