0% found this document useful (0 votes)
20 views12 pages

Python Record

The document contains a series of programming tasks and solutions primarily focused on Python. It includes calculations for compound interest, distance between points, reading user input, printing patterns, checking character types, generating Fibonacci sequences, finding prime numbers, computing GCD, checking for palindromes, and performing matrix operations (addition, subtraction, multiplication). Each task is followed by a sample solution and output demonstration.

Uploaded by

monumallemari1
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)
20 views12 pages

Python Record

The document contains a series of programming tasks and solutions primarily focused on Python. It includes calculations for compound interest, distance between points, reading user input, printing patterns, checking character types, generating Fibonacci sequences, finding prime numbers, computing GCD, checking for palindromes, and performing matrix operations (addition, subtraction, multiplication). Each task is followed by a sample solution and output demonstration.

Uploaded by

monumallemari1
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/ 12

INDEX

sl. pg.
code
no no

Write a program to calculate compound interest when principal, rate and number
1 of periods are 1
given.

2 Given coordinates (x1, y1), (x2, y2) find the distance between two points 1

Read name, address, email and phone number of a person through


3 keyboard and print the details. 2

Print the below triangle using for loop.


5
44
4 2-3
333
2222
11111

Write a program to check whether the given input is digit or lowercase


5 character or uppercase character or a special character (use 'if-else-if' 3
ladder)

6 Python Program to Print the Fibonacci sequence using while loop 4

Python program to print all prime numbers in a given interval (use


7 4-5
break)

Write a function called gcd that takes parameters a and b and returns
8 5
their greatest common divisor.

Write a function called palindrome that takes a string argument and


returnsTrue if it is a palindrome
9 5-6
and False otherwise. Remember that you can use the built-in function len
to check the length of a string.
10 Write a python program that defines a matrix and prints 6

11
Write a python program to perform addition of two square matrices 7-8
a

11
Write a python program to perform subtraction of two square matrices 8-9
b

9-
12 Write a python program to perform multiplication of two square matrices
10
Q1)Write a program to calculate compound interest when principal, rate and number of
periods are
given.
SOL)
def compound_interest(principal, rate, time):
amount = principal * (1 + rate/100)**time
return amount - principal

# Example
p = float(input("Enter Principal: "))
r = float(input("Enter Rate: "))
t = int(input("Enter Time Period: "))
ci = compound_interest(p, r, t)
print(f"Compound Interest: {ci:.2f}")

OUTPUT:-
Enter Principal amount: 10000
Enter Rate of Interest: 5
Enter Time Period (in years): 2
Compound Interest after 2 years: 1025.00

Q2) Given coordinates (x1, y1), (x2, y2) find the distance between two points
SOL)
import math

x1, y1 = map(float, input("Enter x1 and y1: ").split())


x2, y2 = map(float, input("Enter x2 and y2: ").split())

distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)


print(f"Distance = {distance:.2f}")

OUTPUT:-
Enter x1 and y1 (separated by space): 2 3
Enter x2 and y2 (separated by space): 6 7
Distance between the two points = 5.66
Q3)Read name, address, email and phone number of a person through keyboard and print
the details.
SOL)
name = input("Enter Name: ")
address = input("Enter Address: ")
email = input("Enter Email: ")
phone = input("Enter Phone Number: ")

print("\n--- Person Details ---")


print("Name:", name)
print("Address:", address)
print("Email:", email)
print("Phone:", phone)

OUTPUT:-
Enter Name: Sai
Enter Address: Hyderabad
Enter Email: sai@example.com
Enter Phone Number: 9876543210

--- Person Details ---


Name : Sai
Address: Hyderabad
Email : sai@example.com
Phone : 9876543210

Q4)Print the below triangle using for loop.


5
44
333
2222
11111

ANS)for i in range(5, 0, -1):


print((str(i) + " ") * (6 – i))

OUTPUT

5
44
333
2222
11111

Q5)Write a program to check whether the given input is digit or lowercase character or
uppercase
character or a special character (use 'if-else-if' ladder)

ANS)
ch = input("Enter a character: ")

if ch.isdigit():
print("Digit")
elif ch.islower():
print("Lowercase Letter")
elif ch.isupper():
print("Uppercase Letter")
else:
print("Special Character")

OUTPUT
case1:
Enter a single character: A
It is an Uppercase Letter.

Case2:
Enter a single character: g
It is a Lowercase Letter.

Case3:
Enter a single character: 7
It is a Digit.

Case4:
Enter a single character: @
It is a Special Character.
Q6)Python Program to Print the Fibonacci sequence using while loop
ANS)

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


a, b = 0, 1
count = 0

while count < n:


print(a, end=" ")
a, b = b, a + b
count += 1

OUTPUT

Enter the number of Fibonacci terms to print: 7


Fibonacci Sequence:
0112358

Q7)Python program to print all prime numbers in a given interval (use break)
SOL)
start = int(input("Enter start: "))
end = int(input("Enter end: "))

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


if num > 1:
for i in range(2, num):
if num % i == 0:
break
else:
print(num, end=" ")

OUTPUT
Enter the start of interval: 10
Enter the end of interval: 30

Prime numbers between 10 and 30 are:


11 13 17 19 23 29
Q8)Write a function called gcd that takes parameters a and b and returns their greatest
common divisor.
SOL)
def gcd(a, b):
while b:
a, b = b, a % b
return a

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
print("GCD is:", gcd(x, y))

OUTPUT
Enter first number: 48
Enter second number: 18
GCD is: 6

Q9) Write a function called palindrome that takes a string argument and returnsTrue if it is a
palindrome
and False otherwise. Remember that you can use the built-in function len to check the length
of a string.

SOL)
def palindrome(s):
return s == s[::-1]

string = input("Enter a string: ")


print("Palindrome?" , palindrome(string))

OUTPUT
case1:
Enter a string: madam
Palindrome: True
case2:Enter a string: python
Palindrome: False

Q10)Write a python program that defines a matrix and prints


SOL)
# Define matrix dimensions
rows = int(input("Enter number of rows: "))
cols = int(input("Enter number of columns: "))

# Initialize matrix
matrix = []

print("Enter the elements row-wise:")

# Input matrix elements


for i in range(rows):
row = []
for j in range(cols):
value = int(input(f"Enter element at position [{i}][{j}]: "))
row.append(value)
matrix.append(row)

# Display matrix
print("\nMatrix:")
for row in matrix:
print(row)

OUTPUT)
Enter number of rows: 2
Enter number of columns: 3
Enter the elements row-wise:
Enter element at position [0][0]: 1
Enter element at position [0][1]: 2
Enter element at position [0][2]: 3
Enter element at position [1][0]: 4
Enter element at position [1][1]: 5
Enter element at position [1][2]: 6

Matrix:
[1, 2, 3]
[4, 5, 6]

Q11)Write a python program to perform addition and subtraction of two square matrices
SOL)

(i)# Function to read a square matrix


def read_matrix(n, label):
print(f"\nEnter elements of Matrix {label} (row-wise):")
matrix = []
for i in range(n):
row = list(map(int, input(f"Row {i+1}: ").split()))
matrix.append(row)
return matrix

# Function to add two matrices


def add_matrices(a, b):
result = []
for i in range(len(a)):
row = [a[i][j] + b[i][j] for j in range(len(a))]
result.append(row)
return result

# Function to print matrix


def print_matrix(matrix, title):
print(f"\n{title}")
for row in matrix:
print(' '.join(map(str, row)))

# Main code
n = int(input("Enter the order of square matrices: "))
matrix1 = read_matrix(n, 'A')
matrix2 = read_matrix(n, 'B')
added_matrix = add_matrices(matrix1, matrix2)
print_matrix(added_matrix, "Addition of Matrices (A + B):")

(ii)# Function to read a square matrix


def read_matrix(n, label):
print(f"\nEnter elements of Matrix {label} (row-wise):")
matrix = []
for i in range(n):
row = list(map(int, input(f"Row {i+1}: ").split()))
matrix.append(row)
return matrix

# Function to subtract two matrices


def subtract_matrices(a, b):
result = []
for i in range(len(a)):
row = [a[i][j] - b[i][j] for j in range(len(a))]
result.append(row)
return result

# Function to print matrix


def print_matrix(matrix, title):
print(f"\n{title}")
for row in matrix:
print(' '.join(map(str, row)))

# Main code
n = int(input("Enter the order of square matrices: "))
matrix1 = read_matrix(n, 'A')
matrix2 = read_matrix(n, 'B')
subtracted_matrix = subtract_matrices(matrix1, matrix2)
print_matrix(subtracted_matrix, "Subtraction of Matrices (A – B):")

OUTPUT
(i)Enter the order of square matrices: 2

Enter elements of Matrix A (row-wise):


Row 1: 1 2
Row 2: 3 4

Enter elements of Matrix B (row-wise):


Row 1: 5 6
Row 2: 7 8

Addition of Matrices (A + B):


68
10 12

(ii)Enter the order of square matrices: 2

Enter elements of Matrix A (row-wise):


Row 1: 9 8
Row 2: 7 6

Enter elements of Matrix B (row-wise):


Row 1: 1 2
Row 2: 3 4

Subtraction of Matrices (A - B):


86
42

Q12)Write a python program to perform multiplication of two square matrices


SOL)
# Function to read a square matrix
def read_matrix(n, label):
print(f"\nEnter elements of Matrix {label} (row-wise):")
matrix = []
for i in range(n):
row = list(map(int, input(f"Row {i+1}: ").split()))
matrix.append(row)
return matrix

# Function to multiply two square matrices


def multiply_matrices(A, B):
n = len(A)
result = []
for i in range(n):
row = []
for j in range(n):
sum = 0
for k in range(n):
sum += A[i][k] * B[k][j]
row.append(sum)
result.append(row)
return result

# Function to print matrix


def print_matrix(matrix, title):
print(f"\n{title}")
for row in matrix:
print(' '.join(map(str, row)))
# Main code
n = int(input("Enter the order of square matrices: "))

# Read two matrices


matrix1 = read_matrix(n, 'A')
matrix2 = read_matrix(n, 'B')

# Multiply matrices
product_matrix = multiply_matrices(matrix1, matrix2)

# Print result
print_matrix(product_matrix, "Multiplication of Matrices (A x B):")

OUTPUT

Enter the order of square matrices: 2

Enter elements of Matrix A (row-wise):


Row 1: 1 2
Row 2: 3 4

Enter elements of Matrix B (row-wise):


Row 1: 5 6
Row 2: 7 8

Multiplication of Matrices (A x B):


19 22
43 50

You might also like