lab2_for_nested_loop
lab2_for_nested_loop
Write a Python program using a for loop to print the first 10 natural
numbers from 1 to 10.
print(i)
2. Print the Sum of First N Natural Numbers. Accept user input for n, then calculate and print the sum of
the first n natural numbers using a for loop.
print("Sum:", sum_n)
3. Reverse a String. Prompt the user to enter a string, then reverse the string using a for loop.
rev_s = ""
for char in s:
4. Check if a Number is Prime. Write a program that checks if a number entered by the user is a prime
number using nested loops.
if n > 1:
if n % i == 0:
break
else:
print("Prime number")
else:
5. Print Fibonacci Sequence Up to n Terms. Accept input for the number of terms and generate the
Fibonacci sequence using a for loop.
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
6. Count Vowels in a String. Ask the user for a string and count the number of vowels using a for loop.
vowels = "aeiouAEIOU"
7. Multiplication Table for a Number. Accept a number from the user and print its multiplication table up
to 10 using a for loop.
8. Find Factorial of a Number. Ask the user for a number and calculate its factorial using a for loop.
fact *= i
print("Factorial:", fact)
9. Print Elements of a List in Reverse Order. Given a list of integers, print the elements in reverse order
using a for loop.
nums = [1, 2, 3, 4, 5]
for i in reversed(nums):
print(i)
10. Calculate the Sum of Elements in a List. Write a program to calculate and display the sum of
elements in a list using a for loop.
nums = [1, 2, 3, 4, 5]
print("Sum:", sum(nums))
11. Print a Pyramid of Stars. Create a pyramid pattern of stars with a user-defined number of rows.
12. Generate a Matrix with Sequential Numbers. Write a program to generate a 3x3 matrix of sequential
numbers using nested loops.
num = 1
for i in range(3):
for j in range(3):
print()
13. Find Common Elements in Two Lists. Given two lists, write a program to find and print common
elements using nested loops.
list1 = [1, 2, 3, 4, 5]
list2 = [4, 5, 6, 7, 8]
14. Display a Hollow Square Patter. Create a hollow square pattern of size n.
for i in range(n):
for j in range(n):
if i == 0 or i == n-1 or j == 0 or j == n-1:
else:
print()
15. Matrix Multiplication. Write a program to multiply two 2x2 matrices using nested loops.
for i in range(2):
for j in range(2):
for k in range(2):
for row in C:
print(row)
16. Print Alphabet Triangle Pattern. Create a triangle pattern of alphabets with user-defined rows.
17. Sum of Each Row and Column in a Matrix. Compute and display the sum of each row and column in a
matrix using nested loops.
for j in range(3):
18. Check if a Matrix is Symmetric. Write a program to check whether a given square matrix is
symmetric.
19. Print a Hollow Diamond Pattern. Create a hollow diamond pattern with user-defined rows.
n = int(input("Enter size: "))
print(" " * ((n - i) // 2) + "*" + " " * (i - 2) + ("*" if i > 1 else ""))
print(" " * ((n - i) // 2) + "*" + " " * (i - 2) + ("*" if i > 1 else ""))
20. Generate and Print Multiplication Tables from 1 to n. Accept user input for n and generate
multiplication tables from 1 to n using nested loops.
print()