2nd puc computer science lab manual,2nd puc computer science lab programs pdf 2025,2nd puc computer science lab manual 2025 pdf free download,2nd puc computer science lab manual python,2nd puc computer science lab manual 2025 python,2nd puc computer science lab manual 2025-26,2nd puc computer science lab manual karnataka,2nd puc computer science lab manual pdf download,class 12 computer science lab manual,class 12 computer science lab programs,class 12th computer science lab manual,cbse class 12 computer science lab manual,ncert lab manual class 12 computer science, computer science lab manual class 12 pdf free download,class 12 computer science lab,class 12 computer science practical book solutions,class 12 computer science practical program,
1. Write a python program using a function to print n Fibonacci numbers.
def fibo(n):
first = 0
second = 1
for i in range(n):
print(first, end=' ')
third = first + second
first = second
second = third
n = int(input("How many Fibonacci numbers do you want to print? "))
print("Fibonacci sequence:")
fibo(n)
2. Write a menu driven program in python to find factorial and sum of natural of n Numbers using function.
def find_factorial(n):
factorial = 1
for i in range(1, n + 1):
factorial *= i
return factorial
def sum_natural_numbers(n):
sum = 0
for i in range(1, n + 1):
sum += i
return sum
while True:
print("\n--- MENU ---")
print("1. Find Factorial of a number")
print("2. Find Sum of n Natural Numbers")
print("3. Exit")
choice = int(input("Enter your choice (1-3): "))
if choice == 1:
num = int(input("Enter a number to find factorial: "))
if num < 0:
print("Factorial is not defined for negative numbers.")
else:
result = find_factorial(num)
print("Factorial of ",num," is ",result)
elif choice == 2:
num = int(input("Enter a number to find sum of natural numbers: "))
if num < 1:
print("Please enter a number greater than 0.")
else:
result = sum_natural_numbers(num)
print("Sum of first ",num," natural numbers is ",result)
elif choice == 3:
print("Thanks for using the program. Goodbye!")
break
else:
print("Invalid choice! Please enter 1, 2 or 3.")
3. Write a python program using user defined function to calculate interest amount using simple interest method and compound interest method and find the difference of interest amount between the two methods.
def calculate_simple_interest(principal, rate, time):
si = (principal * rate * time) / 100
return si
def calculate_compound_interest(principal, rate, time):
amount = principal * (1 + rate / 100) ** time
ci = amount - principal
return ci
print("Welcome to the Interest Calculator!")
p = float(input("Enter the principal amount (₹): "))
r = float(input("Enter the annual interest rate (%): "))
t = int(input("Enter the time in years: "))
simple_interest = calculate_simple_interest(p, r, t)
compound_interest = calculate_compound_interest(p, r, t)
difference = compound_interest - simple_interest
print("\nResults:")
print("Simple Interest = ₹%.2f" %simple_interest)
print("Compound Interest = ₹%.2f" %compound_interest)
print("Difference = ₹%.2f" %difference)
4. Write a Python Program to create a text file and to read a text file and display the number of vowels, consonants, uppercase and lowercase characters in the file.
with open("sample.txt", "w") as file:
sentence=input("Enter the sentence:")
file.write(sentence)
vowels_list = "aeiouAEIOU"
vowels = 0
consonants = 0
uppercase = 0
lowercase = 0
with open("sample.txt", "r") as file:
text = file.read()
for char in text:
if char.isalpha():
if char in vowels_list:
vowels += 1
else:
consonants += 1
if char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1
print("Number of vowels:", vowels)
print("Number of consonants:", consonants)
print("Number of uppercase letters:", uppercase)
print("Number of lowercase letters:", lowercase)
5.Write a python code to find the number of lines, number of words and number of characters in a text file.
filename = input("Enter the file name (with .txt extension): ")
try:
with open(filename, 'r') as file:
text = file.read()
lines = text.split('\n')
number_of_lines = len(lines)
words = text.split()
number_of_words = len(words)
number_of_characters = len(text)
print("Number of lines:", number_of_lines)
print("Number of words:", number_of_words)
print("Number of characters:", number_of_characters)
except FileNotFoundError:
print("File not found. Please make sure the file exists.")
6. Write a python program to create and to read records in binary file with student name and marks of six subjects.
import pickle
def create_student_records():
students = []
n = int(input("How many student records do you want to enter? "))
for i in range(n):
print("\nEnter details for Student:",i+1)
name = input("Name: ")
marks = []
for j in range(6):
mark = float(input("Enter marks for Subject:"+str(j+1)))
marks.append(mark)
student = {'name': name, 'marks': marks}
students.append(student)
with open("students.dat", "wb") as file:
pickle.dump(students, file)
print("\nStudent records saved successfully!\n")
def read_student_records():
try:
with open("students.dat", "rb") as file:
students = pickle.load(file)
print("Student Records from File:\n")
for student in students:
print("Name:",student['name'])
print("Marks:", student['marks'])
print("Total:",sum(student['marks']))
print("-" * 30)
except FileNotFoundError:
print("File not found. Please create records first.")
while True:
print("\n1. Create Student Records")
print("2. Read Student Records")
print("3. Exit")
choice = int(input("Enter your choice (1-3): "))
if choice == 1:
create_student_records()
elif choice == 2:
read_student_records()
elif choice == 3:
print("Exiting program.")
break
else:
print("Invalid choice. Please enter 1, 2, or 3.")
7. Write a python program to copy the records of the students having percentage 90 and above
from the binary file into another file.
import pickle
students = [
{"name": "Alice", "percentage": 95},
{"name": "Bob", "percentage": 88},
{"name": "Charlie", "percentage": 91},
{"name": "David", "percentage": 76},
{"name": "Eva", "percentage": 90}
]
with open("students.dat", "wb") as file:
for student in students:
pickle.dump(student, file)
print("All student records saved in 'students.dat'.")
with open("students.dat", "rb") as infile, open("top_students.dat", "wb") as outfile:
try:
while True:
student = pickle.load(infile)
if student["percentage"] >= 90:
pickle.dump(student, outfile)
except EOFError:
pass
print("Students with 90% and above copied to 'top_students.dat'.")
with open("top_students.dat", "rb") as file:
print("\nStudents with 90% and above:")
try:
while True:
student = pickle.load(file)
print(student)
except EOFError:
pass
8. Write a python program using function to sort the elements of a list using bubble sort method
def bubblesort(numList):
n = len(numList)
i = 0
while i < n:
j = 0
while j < n - i - 1:
if numList[j] > numList[j + 1]:
numList[j], numList[j + 1] = numList[j + 1], numList[j]
j += 1
i += 1
numList = [8, 7, 13, 1, -9, 4]
print("The original list is:", numList)
bubblesort(numList)
print("The sorted list is:", numList)
9. Write a python program using function to sort the elements of a list using selection sort method
def selectionsort(numList):
n = len(numList)
i = 0
while i < n:
min = i
flag = 0 # Flag to check if a smaller element is found
j = i + 1
while j < n:
if numList[j] < numList[min]:
min = j
flag = 1 # Smaller element found, set flag to True
j += 1
if flag==1:
numList[i], numList[min] = numList[min], numList[i]
i += 1
numList = [8, 7, 13, 1, -9, 4]
print("Original list:", numList)
selectionsort(numList)
print("Sorted list:", numList)
10. Write a python program using function to sort the elements of a list using insertion sort
method
def insertionsort(numList):
n=len(numList)
i = 1
while i < n:
temp = numList[i]
j = i-1
while j >= 0 and numList[j ] > temp:
numList[j+1] = numList[j]
j -= 1
numList[j+1] = temp
i += 1
numbers = [8, 7, 13, 1, -9, 4]
print("Original list:", numbers)
insertionsort(numbers)
print("Sorted list: ", numbers)
11. Write a python program using function to search an element in a list using linear search
method
def linearsearch(numList, key):
n=len(numList)
index = 0
while index < n:
if numList[index] == key:
return index + 1
else:
index = index+1
return -1
numList = [10, 25, 30, 45, 50, 65, 70]
key = int(input("Enter the number you want to search: "))
result = linearsearch(numList, key)
if result >= 0:
print("The number ",key," is found at position: ",result)
else:
print("Search is unsuccessful")
Note: Upcoming programs will be available shortly. Please stay connected as this page will be updated soon. Thank you for your patience and continued support.

No comments: