0% found this document useful (0 votes)
7 views2 pages

8 Program

Uploaded by

aumjha24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views2 pages

8 Program

Uploaded by

aumjha24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

a) Program to multiply two matrices using nested loops

b) Python Program to Print all the Prime Numbers within a Given Range

Program to multiply two matrices using nested loops

# Get matrix dimensions from the user


rows_A = int(input("Enter the number of rows in Matrix A: "))
cols_A = int(input("Enter the number of columns in Matrix A: "))
rows_B = int(input("Enter the number of rows in Matrix B: "))
cols_B = int(input("Enter the number of columns in Matrix B: "))

# Ensure that the matrices can be multiplied


if cols_A != rows_B:
print("Error: Number of columns in Matrix A must be equal to the number of rows in
Matrix B.")
else:
# Initialize matrices A and B
print("Enter elements of Matrix A:")
matrix_A = [[int(input(f"Enter element A[{i+1}][{j+1}]: ")) for j in range(cols_A)] for i in
range(rows_A)]

print("Enter elements of Matrix B:")


matrix_B = [[int(input(f"Enter element B[{i+1}][{j+1}]: ")) for j in range(cols_B)] for i in
range(rows_B)]

# Initialize the result matrix with zeros


result = [[0 for _ in range(cols_B)] for _ in range(rows_A)]

# Multiply matrices A and B


for i in range(rows_A):
for j in range(cols_B):
for k in range(cols_A):
result[i][j] += matrix_A[i][k] * matrix_B[k][j]

# Print the result


print("Resultant Matrix after multiplication:")
for row in result:
print(row)
Python Program to Print all the Prime Numbers within a Given Range

# Get the range from the user


start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))

print(f"Prime numbers between {start} and {end} are:")

# Iterate through each number in the range


for num in range(start, end + 1):
# Prime numbers are greater than 1
if num > 1:
is_prime = True
for i in range(2, int(num ** 0.5) + 1):
if num % i == 0:
is_prime = False
break
if is_prime:
print(num)

Additional Clarity for the written Program, only for understanding, not for writing:

How is_prime Works

1. Initially, is_prime is set to True for each number (num) in the range.
2. The code then checks if num has any divisors other than 1 and itself by iterating from
2 to sqrt(num).
3. If it finds a divisor (i.e., num % i == 0), it sets is_prime to False, meaning that num is
not a prime number, and the loop stops.
4. After the loop, if is_prime is still True, it means num has no divisors other than 1 and
itself, so it’s confirmed as a prime number and printed.

In the prime-checking logic,


range(2, int(num ** 0.5) + 1)
optimizes the program by checking divisors only up to the square root of num.
This works because if num has a factor greater than its square root,
it must also have a factor smaller than its square root.
This optimization makes the program more efficient, especially for larger numbers.

You might also like