0% found this document useful (0 votes)
3 views

lab2_for_nested_loop

The document contains a series of Python programming exercises that cover various fundamental concepts such as loops, conditionals, and data structures. Each exercise includes a brief description and the corresponding code to implement the task, ranging from printing natural numbers to generating multiplication tables and checking matrix properties. The exercises are designed to help users practice and enhance their programming skills.

Uploaded by

aninepacunana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

lab2_for_nested_loop

The document contains a series of Python programming exercises that cover various fundamental concepts such as loops, conditionals, and data structures. Each exercise includes a brief description and the corresponding code to implement the task, ranging from printing natural numbers to generating multiplication tables and checking matrix properties. The exercises are designed to help users practice and enhance their programming skills.

Uploaded by

aninepacunana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

1. Print First 10 Natural Numbers.

Write a Python program using a for loop to print the first 10 natural
numbers from 1 to 10.

for i in range(1, 11):

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.

n = int(input("Enter a number: "))

sum_n = sum(range(1, n + 1))

print("Sum:", sum_n)

3. Reverse a String. Prompt the user to enter a string, then reverse the string using a for loop.

s = input("Enter a string: ")

rev_s = ""

for char in s:

rev_s = char + rev_s

print("Reversed string:", rev_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.

n = int(input("Enter a number: "))

if n > 1:

for i in range(2, int(n**0.5) + 1):

if n % i == 0:

print("Not a prime number")

break

else:
print("Prime number")

else:

print("Not a prime number")

5. Print Fibonacci Sequence Up to n Terms. Accept input for the number of terms and generate the
Fibonacci sequence using a for loop.

n = int(input("Enter number of terms: "))

a, b = 0, 1

for _ in range(n):

print(a, end=" ")

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.

s = input("Enter a string: ")

vowels = "aeiouAEIOU"

count = sum(1 for char in s if char in vowels)

print("Number of vowels:", count)

7. Multiplication Table for a Number. Accept a number from the user and print its multiplication table up
to 10 using a for loop.

n = int(input("Enter a number: "))

for i in range(1, 11):

print(n, "x", i, "=", n * i)

8. Find Factorial of a Number. Ask the user for a number and calculate its factorial using a for loop.

n = int(input("Enter a number: "))


fact = 1

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

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.

n = int(input("Enter number of rows: "))

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

print(" " * (n - i) + "* " * i)

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(num, end=" ")


num += 1

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]

common = [i for i in list1 if i in list2]

print("Common elements:", common)

14. Display a Hollow Square Patter. Create a hollow square pattern of size n.

n = int(input("Enter size: "))

for i in range(n):

for j in range(n):

if i == 0 or i == n-1 or j == 0 or j == n-1:

print("*", end=" ")

else:

print(" ", end=" ")

print()

15. Matrix Multiplication. Write a program to multiply two 2x2 matrices using nested loops.

A = [[1, 2], [3, 4]]

B = [[5, 6], [7, 8]]

C = [[0, 0], [0, 0]]

for i in range(2):

for j in range(2):
for k in range(2):

C[i][j] += A[i][k] * B[k][j]

for row in C:

print(row)

16. Print Alphabet Triangle Pattern. Create a triangle pattern of alphabets with user-defined rows.

n = int(input("Enter number of rows: "))

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

print(" " * (n - i) + "".join(chr(65 + j) for j in range(i)))

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.

matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for row in matrix:

print("Row sum:", sum(row))

for j in range(3):

col_sum = sum(matrix[i][j] for i in range(3))

print("Column", j + 1, "sum:", col_sum)

18. Check if a Matrix is Symmetric. Write a program to check whether a given square matrix is
symmetric.

matrix = [[1, 2, 3], [2, 4, 5], [3, 5, 6]]

symmetric = all(matrix[i][j] == matrix[j][i] for i in range(len(matrix)) for j in range(len(matrix)))

print("Symmetric" if symmetric else "Not symmetric")

19. Print a Hollow Diamond Pattern. Create a hollow diamond pattern with user-defined rows.
n = int(input("Enter size: "))

for i in range(1, n, 2):

print(" " * ((n - i) // 2) + "*" + " " * (i - 2) + ("*" if i > 1 else ""))

for i in range(n-2, 0, -2):

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.

n = int(input("Enter a number: "))

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

print(f"Multiplication Table of {i}:")

for j in range(1, 11):

print(i, "x", j, "=", i * j)

print()

You might also like