Grade 11 Java – Nested Loop Assignment (20 Questions)
Part A: Understanding Nested Loops
1. What is a nested loop in Java?
The inner loop runs completely every time the outer loop runs.
2. How many times will the inner loop execute if the outer loop runs 5 times and the inner
loop runs 4 times?
5 x 4 = 20 times
3. Write the general syntax of a nested for loop in Java.
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 3; j++) {
[Link]("Hello");
}
}
4. Can a while loop be nested inside a for loop? Explain.
Yes. Any loop can be inside another loop.
→Example of code:
for(int i = 1; i <= 3; i++) {
int j = 1;
while(j <= 2) {
[Link]("Nested Loop");
J++;
}
}
5. What happens if the inner loop never becomes false?
The program will run forever (infinite loop) and never stop.
Part B: Pattern Printing (Using Nested Loops)
Write Java programs for the following:
6. Print a square of stars (5 × 5):
*****
*****
*****
*****
*****
Code:
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
[Link]("*");
}
[Link]();
}
7. Print a right-angled triangle:
*
**
***
****
*****
Code:
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
8. Print an inverted right-angled triangle:
*****
****
***
**
*
Code:
for(int i = 5; i >= 1; i--) {
for(int j = 1; j <= i; j++) {
[Link]("*");
}
[Link]();
}
9. Print the following number pattern:
1
12
123
1234
12345
Code:
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
[Link](j);
}
[Link]();
}
10.Print this pattern:
5
54
543
5432
54321
Code:
for(int i = 5; i >= 1; i--) {
for(int j = 5; j >= i; j--) {
[Link](j);
}
[Link]();
}
Part C: Number-Based Nested Loop Programs
11.Display a multiplication table from 1 to 5 using nested loops.
Code:
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
[Link](i * j + " ");
}
[Link]();
12.Print all pairs (i, j) where i runs from 1 to 3 and j runs from 1 to 4.
Code:
for(int i = 1; i <= 3; i++) {
for(int j = 1; j <= 4; j++) {
[Link]("(" + i + ", " + j + ")");
}
}
13.Count how many times the inner loop runs if:
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= i; j++) {
[Link]("*");
}
}
→Total: 10 times
14.Write a program to print all even numbers between 1 and 10 using nested loops.
Code:
for(int i = 1; i <= 10; i++) {
for(int j = 1; j <= 1; j++) {
if(i % 2 == 0) {
[Link](i);
}
}
}
15.Using nested loops, display numbers from 1 to 25 in a 5 × 5 grid.
Code:
int num = 1;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= 5; j++) {
[Link](num + " ");
num++;
}
[Link]();
}
Part D: Logic & Application
16.Write a program to print the following pattern:
1
22
333
4444
55555
Code:
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
[Link](i);
}
[Link]();
}
17.Write a Java program to display the following pattern:
A
BB
CCC
DDDD
EEEEE
char letter = 'A';
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
[Link](letter);
}
[Link]();
letter++;
}
18.Find and display all factors of numbers from 1 to 10 using nested loops.
Code:
for(int i = 1; i <= 10; i++) {
[Link]("Factors of " + i + ": ");
for(int j = 1; j <= i; j++) {
if(i % j == 0) {
[Link](j + " ");
}
}
[Link]();
}
19.Write a program to print Floyd’s Triangle using nested loops.
Code:
int num = 1;
for(int i = 1; i <= 5; i++) {
for(int j = 1; j <= i; j++) {
[Link](num + " ");
num++;
}
[Link]();
}
20.Explain one real-life example where nested loops are useful.
In a school, one loop can go through each class, and another loop inside it can go through each
student in that class.
Nested loops are useful in real life when we have groups with items inside them and need to
repeat an action for every item in every group. For example, in a school, there are many classes
and each class has many students. An outer loop can be used to go through each class, while
an inner loop goes through each student in that class. The inner loop completes its work for all
students before the program moves on to the next class. This makes nested loops very helpful
for organizing and processing structured data, such as classrooms and students, rows and
seats in a theater, or shelves and products in a store.
Code (INPUT):
for(int classNum = 1; classNum <= 3; classNum++) {
for(int student = 1; student <= 5; student++) {
[Link]("Class " + classNum + " Student " + student);
}
}
Code (OUTPUT):
Class 1 Student 1
Class 1 Student 2
Class 1 Student 3
Class 1 Student 4
Class 1 Student 5
Class 2 Student 1
Class 2 Student 2
Class 2 Student 3
Class 2 Student 4
Class 2 Student 5
Class 3 Student 1
Class 3 Student 2
Class 3 Student 3
Class 3 Student 4
Class 3 Student 5