You are on page 1of 12

LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques – 1) Write a program that will determine whether a point lies inside the circle, on the
circle or outside the circle for given the coordinates(x,y) of the center of a circle and its
radius. (Use sort () and pow () functions)
Ans -import math
def point_position(x, y, center_x, center_y, radius):
distance = math.sqrt(pow(x - center_x, 2) + pow(y - center_y, 2))

if distance < radius:


return "Inside the circle"
elif distance == radius:
return "On the circle"
else:
return "Outside the circle"
def main():
# Input the coordinates of the center and radius of the circle
center_x = float(input("Enter the x-coordinate of the center: "))
center_y = float(input("Enter the y-coordinate of the center: "))
radius = float(input("Enter the radius of the circle: "))

# Input the coordinates of the point


x = float(input("Enter the x-coordinate of the point: "))
y = float(input("Enter the y-coordinate of the point: "))

# Determine the position of the point relative to the circle


result = point_position(x, y, center_x, center_y, radius)

# Output the result


print(f"The point ({x}, {y}) is {result} with respect to the circle.")
if __name__ == "__main__":
main()

Enrollment no. 0103CA221095 1|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Output - Enter the x-coordinate of the center:


Ques -2) Develop a Python program that solves a quadratic equation of the form ax^2 + bx
+c=0.

1. Ans - # import complex math module


2. import cmath
3. a = float(input('Enter a: '))
4. b = float(input('Enter b: '))
5. c = float(input('Enter c: '))
6.
7. # calculate the discriminant
8. d = (b**2) - (4*a*c)
9.
10. # find two solutions
11. sol1 = (-b-cmath.sqrt(d))/(2*a)
12. sol2 = (-b+cmath.sqrt(d))/(2*a)
13. print('The solution are {0} and {1}'.format(sol1,sol2))

Output:

Enter a: 8
Enter b: 5
Enter c: 9
The solution are (-0.3125-1.0135796712641785j) and (-0.3125+1.0135796712641785j)
c

Enrollment no. 0103CA221095 2|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques – 3) Write a python program to check whether a triangle is isosceles, equilateral, scalene or
right angled triangle for the given three sides of a triangle

Ans - # Display a message prompting the user to input lengths of the sides of a triangle

print("Input lengths of the triangle sides: ")

# Request input from the user for the length of side 'x' and convert it to an integer

x = int(input("x: "))

# Request input from the user for the length of side 'y' and convert it to an integer

y = int(input("y: "))

# Request input from the user for the length of side 'z' and convert it to an integer

z = int(input("z: "))

# Check conditions to determine the type of triangle based on the lengths of its sides

# If all sides are equal, display that it's an equilateral triangle

if x == y == z:

print("Equilateral triangle")

# If at least two sides are equal, display that it's an isosceles triangle

elif x == y or y == z or z == x:

print("Isosceles triangle")

# If all sides have different lengths, display that it's a scalene triangle

else:

print("Scalene triangle")

output – x : 6
y:8
z : 12

Enrollment no. 0103CA221095 3|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

scalene trinagle

Enrollment no. 0103CA221095 4|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques – 4 ) Design a function named Print Unique () in python which will return a list of
unique elements in sorted order
Ans - # function to get unique values
def unique(list1):

# initialize a null list


unique_list = []

# traverse for all elements


for x in list1:
# check if exists in unique_list or not
if x not in unique_list:
unique_list.append(x)
# print list
for x in unique_list:
print x,

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
unique(list1)

list2 = [1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
unique(list2)
Output : the unique value from 1st list is
10 20 30 40
the unique value from list 2nd ids
1 2 3 4 5

Enrollment no. 0103CA221095 5|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques -5) Develop a python script to count the number of alphabets, digits and special characters in
a string

Ans - # Python program to Count Alphabets Digits and Special Characters in a String

string = input("Please Enter your Own String : ")

alphabets = digits = special = 0

for i in range(len(string)):

if(string[i].isalpha()):

alphabets = alphabets + 1

elif(string[i].isdigit()):

digits = digits + 1

else:

special = special + 1

print("\nTotal Number of Alphabets in this String : ", alphabets)

print("Total Number of Digits in this String : ", digits)

print("Total Number of Special Characters in this String : ", special)

output : Please Enter your Own String : abc!@ 12 cd 1212

Total Number of Alphabets in this String : 5

Total Number of Digits in this String : 6

Total Number of Special Characters in this String : 5

Enrollment no. 0103CA221095 6|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques – 6) Develop a Python program that reads a string from the keyboard and creates a
dictionary containing frequency of each character occurring in the string
Ans - # Define a function named char_frequency that takes one argument, str1.

def char_frequency(str1):

# Initialize an empty dictionary named 'dict' to store character frequencies.

dict = {}

# Iterate through each character 'n' in the input string str1.

for n in str1:

# Retrieve the keys (unique characters) in the 'dict' dictionary.

keys = dict.keys()

# Check if the character 'n' is already a key in the dictionary.

if n in keys:

# If 'n' is already a key, increment its value (frequency) by 1.

dict[n] += 1

else:

# If 'n' is not a key, add it to the dictionary with a frequency of 1.

dict[n] = 1

# Return the dictionary containing the frequency of each character in the input string.

return dict

# Call the char_frequency function with the argument 'google.com' and print the result.

print(char_frequency('google.com'))

Sample Output:

{'g': 2, 'o': 3, 'l': 1, 'e': 1, '.': 1, 'c': 1, 'm': 1}

Enrollment no. 0103CA221095 7|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques – 7) Write a program that creates and uses a Time class to perform various time
arithmetic operations

Ans - # Simple Python program to understand the arithmetical operator addition


# Here, we are storing the first input numbers in num1
num1 = input('Enter first number: ')
# Here, we are storing the second input numbers in num2
num2 = input('Enter second number: ')
# Here, we are declaring a variable sum to store the result
# Here, we are using the arithmetical operator to add the two numbers
sum = float(num1) + float(num2)
# Here, we are printing the sum of the given two numbers
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))

Output : Enter first number: 10


Enter second number: 20
The sum of 10 and 20 is 30.0

Enrollment no. 0103CA221095 8|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques 8 )Write a program that implements a Matrix Class and perform addition,
multiplication, and transpose operations on 3*3matrices
Ans - # Python program to perform matrix operations ,matrix addition, matrix subtraction,
matrix multiplication – addition

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


mat2 = [[1, 2], [3, 4]]
mat3 = [[0, 0], [0, 0]]

for i in range(0, 2):


for j in range(0, 2):
mat3[i][j] = mat1[i][j] + mat2[i][j]

print(“Addition of two matrices”)


for i in range(0, 2):
for j in range(0, 2):
print(mat3[i][j], end = ” “)
print()
Output : 2 2 (order of the matrix)
1 2 3 4 (matrix 1 element)
2 3 4 5 (matrix 2 element)
3 5 (resultant matrix)
79

Enrollment no. 0103CA221095 9|Page


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques -9) Write a program that merges lines alternatively from two files and write the results
to new file. If one file has less number of lines than the other, then remaining lines from the
larger file should be simply copied into target file.
Ans - data = data2 = "";

# Reading data from file1


with open('file1.txt') as fp:
data = fp.read()

# Reading data from file2


with open('file2.txt') as fp:
data2 = fp.read()

# Merging 2 files
# To add the data of file2
# from next line
data += "\n"
data += data2

with open ('file3.txt', 'w') as fp:


fp.write(data)
Output : This is the content from file 1
Hello there
This is in file 1
Hello again
This is in file 2

Enrollment no. 0103CA221095 10 | P a g e


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Ques -10 ) Write a program to read the file and print a list of all blood donors whose age is
below 25 and blood type is 3
Ans - def read_file(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
return lines

def filter_donors(lines):
donors = []
for line in lines[1:]: # Skipping the header line
name, age, blood_type = line.strip().split(', ')
age = int(age)
blood_type = int(blood_type)
if age < 25 and blood_type == 3:
donors.append(name)
return donors
def main():
file_path = 'path/to/your/file.txt' # Replace with the actual path to your file
lines = read_file(file_path)
donors = filter_donors(lines)
if donors:
print("List of blood donors below 25 years old with blood type 3:")
for donor in donors:
print(donor)
else:
print("No matching donors found.")
if __name__ == "__main__":
main()
Output – blood donor
Age – 25

Enrollment no. 0103CA221095 11 | P a g e


LNCT – MCA ,Bhopal Name :- Lakshya Agnihotri

Group - 3

Enrollment no. 0103CA221095 12 | P a g e

You might also like