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++;
}
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;
}
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;
}
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;
}
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;
}
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)