You are on page 1of 5

Nested Loops

Questions:
1. Why do we use nested loops in programming?
2. How can you use two nested loops to print a square of stars?
3. What is the difference between nested for loops and nested while loops?
4. How can we use two nested loops to print a right-angled triangle using numbers?
5. How can you use nested loops to generate the multiplication table up to a certain limit?
6. Explain the concept of a nested loop with an example.
7. Write a Python program using nested loops to print the following pattern:
*
**
***
****
8. How can you use nested loops to create a chessboard pattern of 0 and 1?
9. Write a Python program to find and print all pairs of elements in an array whose sum is
equal to a given target.
10. What are some common pitfalls to avoid when working with nested loops?
11. Write a Python program to calculate and print the factorial of a given number using
nested loops.
12. Explain the term "loop iteration" in the context of nested loops.
13. How can you use nested loops to print a pattern of alphabets like the following:
A
BB
CCC
DDDD
14. Write a Python program to find the prime numbers in a given range using nested loops.
15. What are the advantages of using nested loops over a single loop in certain scenarios?
16. Sorting using nested loops.

Answers:
1. Explanation:
Nested loops are used when we need to perform repetitive tasks within another
set of repetitive tasks. They are helpful for working with 2D data structures or for
solving problems that involve multiple levels of iteration.
2. Example:
You can use two nested for loops to iterate over rows and columns to print a
square of stars.
for i in range(4):
for j in range(4):
print('*', end=' ')

print()
3. Explanation:
The main difference is in their syntax and use cases. for loops are typically used
when the number of iterations is known, while while loops are used when the
condition for termination is known.
4. Example:
• You can use nested loops to print a right-angled triangle of numbers.
for i in range(1, 6):

for j in range(1, i + 1):


print(j, end=' ')
print()
5. Example:
• You can use nested loops to generate a multiplication table.

for i in range(1, 6):


for j in range(1, 6):
print(i * j, end='\t')
print()
6. Explanation:
• A nested loop is a loop inside another loop. It involves repeating a set of
statements or instructions within another set of statements or instructions.
7. Example:

• Program to print a pattern of stars.


for i in range(1, 5):
for j in range(i):
print('*', end=' ')
print()
8. Example:
• Program to create a chessboard pattern.

for i in range(8):
for j in range(8):
print((i + j) % 2, end=' ')
print()

9. Example:
• Program to find pairs with a given sum.
arr = [1, 2, 3, 4, 5]
target_sum = 6

for i in range(len(arr)):
for j in range(i + 1, len(arr)):
if arr[i] + arr[j] == target_sum:
print(f'Pair: ({arr[i]}, {arr[j]})')

10. Explanation:
• Common pitfalls include forgetting to update loop variables, creating infinite
loops, or incorrectly using loop control statements.
11. Example:
• Program to calculate the factorial.
n=5
result = 1
for i in range(1, n + 1):

result *= i

12. Explanation:
• Loop iteration refers to the process of repeatedly executing a set of statements
or instructions within a loop.
13. Example:

• Program to print a pattern of alphabets.


for i in range(4):
for j in range(i + 1):
print(chr(65 + i), end=' ')

print()
14. Example:
• Program to find prime numbers in a range.
# Input range

start = 10
end = 50

# Find prime numbers in the range


print("Prime numbers:")

for num in range(start, end + 1):


if num > 1:
for i in range(2, int(num**0.5) + 1):
if num % i == 0:
break
else:
print(num)
15. Explanation:

• Nested loops are advantageous in scenarios where multiple levels of iteration are
needed, such as working with 2D arrays or matrices. They allow for more
complex patterns and calculations.
16. Sorting using nested loops
# Input list
numbers = [5, 2, 9, 1, 5, 6]

# Sorting using nested loops (Bubble Sort)


n = len(numbers)
for i in range(n):
for j in range(0, n-i-1):

# Swap if the element found is greater than the next element


if numbers[j] > numbers[j+1]:
numbers[j], numbers[j+1] = numbers[j+1], numbers[j]

# Output sorted list


print("Sorted numbers:", numbers)

You might also like