Compute The Greatest Common Divisor and Least Common Multiple of Two Integers
Compute The Greatest Common Divisor and Least Common Multiple of Two Integers
while b:
a, b = b, a % b
return a
return (a * b) // calculate_gcd(a, b)
# Input integers
num1 = 36
num2 = 48
def fibonacci_series(limit):
a, b = 0, 1
a, b = b, a + b
fibonacci_series(series_limit)
1. Write a menu driven program to enter a number and perform following using
functions :
a. Check if it is a perfect number.
b. Check if it is palindrome
c. Armstrong number
d. Print sum of its digit
e. Print its factorial
def is_perfect_number(num):
sum_divisors = 0
if num % i == 0:
sum_divisors += i
def is_palindrome(num):
def is_armstrong_number(num):
order = len(str(num))
temp = num
sum_armstrong = 0
digit = temp % 10
temp //= 10
def sum_of_digits(num):
def factorial(num):
if num == 0 or num == 1:
return 1
else:
# Menu function
def menu():
print("Menu:")
print("f. Exit")
# Main program
while True:
menu()
if choice == 'a':
if is_perfect_number(number):
else:
else:
if is_armstrong_number(number):
else:
break
else:
1. WAP to implement a function L_Search that take input as a list and implement
searching using linear Search.
found = False
for i in range(len(arr)):
break
# Input list
search_element = 56
# Display result
if is_found:
else:
5. WAP to implement a function B_Search that take input as a list and implement searching
using Binary Search.
low = 0
high = len(arr) - 1
if arr[mid] == target:
return mid
else:
high = mid - 1
# Example usage:
target_element = 12
if result != -1:
else:
def bubble_sort(arr):
n = len(arr)
for i in range(n):
# Traverse the array from 0 to n-i-1. Swap if the element found is greater
# Example usage:
n = len(arr)
for i in range(n):
min_index = i
min_index = j
# Example usage:
selection_sort(my_list)
def insertion_sort(arr):
key = arr[i]
# Move elements of arr[0..i-1], that are greater than key, to one position ahead
j=i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
# Example usage:
insertion_sort(my_list)
9.Create a dictionary with the roll number, name and marks of n students in a class and display the
names of students who have scored marks above 75.print("Sorted list:", my_list)
students = {
# Function to count vowels, consonants, uppercase, and lowercase characters in a text file
def count_chars(filename):
vowels = 'aeiouAEIOU'
consonants = 'bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ'
text = file.read()
# Example usage
file_path = 'sample.txt' # Replace 'sample.txt' with the path to your text file
2.Read a text file line by line and display each word separated by a #. # Function to read a text file
line by line and display words separated by #
def display_words(file_name):
words = line.split()
print(formatted_line)
# Example usage
file_path = 'sample.txt' # Replace 'sample.txt' with the path to your text file
display_words(file_path)
3.Read a text file and count total number of occurrence of word ‘To’ and ‘TO’
def count_to_occurrences(file_name):
count_to = 0
text = file.read()
# Split the text into words and count occurrences of 'To' and 'TO'
words = text.split()
count_to += words.count('To')
count_to += words.count('TO')
return count_to
# Example usage
file_path = 'sample.txt' # Replace 'sample.txt' with the path to your text file
total_to_count = count_to_occurrences(file_path)
4.Read a text file and count total number of words starting with ‘M’# Function to count words
starting with 'M' in a text file
def count_words_starting_with_m(file_name):
count_m_words = 0
text = file.read()
# Split the text into words and count words starting with 'M'
words = text.split()
return count_m_words
# Example usage
file_path = 'sample.txt' # Replace 'sample.txt' with the path to your text file
total_m_words_count = count_words_starting_with_m(file_path)
print(f"Total words starting with 'M': {total_m_words_count}")
5. Write a program to open a file “Student.txt” in write mode. Store names of the five students
along with the marks in English, Maths and Computer Science in this fill
students_data = [
file_name = 'Student.txt'
6.Write a program in Python to create a text file ‘Note.txt’ to write few lines into it. If you don’t want
more lines then enter 0(zero) to quit.m
def count_words_starting_with_m(file_name):
count_m_words = 0
text = file.read()
# Split the text into words and count words starting with 'M'
words = text.split()
# Example usage
file_path = 'sample.txt' # Replace 'sample.txt' with the path to your text file
total_m_words_count = count_words_starting_with_m(file_path)
7.Write a program that reads characters from the keyboard one by one. All lower case characters get
stored inside the file LOWER, all upper case characters get stored inside the file UPPER and all other
characters get stored inside file OTHERS`
def categorize_characters():
break
if char.islower():
lower_file.write(char + '\n')
elif char.isupper():
upper_file.write(char + '\n')
else:
others_file.write(char + '\n')
# Call the function to categorize characters
categorize_characters()
8. Write a function display to open the same file ‘Student.txt’ in read mode which will retrieve
and display all the records available in the file “Student.txt
def display(file_name):
try:
records = file.readlines()
except FileNotFoundError:
# Example usage
display(file_path
9. Write a program that copies a text file “source.txt” onto “target.txt” barring the lines starting
with “@” sign.
# Function to copy contents from source to target excluding lines starting with '@'
try:
if not line.startswith('@'):
target.write(line)
print(f"Contents copied from {source_file} to {target_file} excluding lines starting with '@'")
except FileNotFoundError:
10. Remove all the lines that contain the character 'a' in a file and write it to another file.
try:
file_out.write(line)
except FileNotFoundError:
# Example usage
remove_lines_with_a(input_file_path, output_file_path)
11. Write a function Line() in Python to use the previous text ile (Notebook.txt) in appropriate
mode to retrieve all the lines. Count the number of lines available in the file and display all the with
line number.
# Function to retrieve all lines from the file and display with line numbers
def Line(file_name):
try:
lines = file.readlines()
line_count = len(lines)
except FileNotFoundError:
# Example usage
Line(file_path)
Arvind 7258031
Sachin 7259197
The names contain only one word the names and telephone numbers are separated by white spaces
Write program to read a file and display its contents in two columns.
def display_two_columns(file_name):
try:
print(f"{name.ljust(15)}{number}")
except FileNotFoundError:
# Example usage
display_two_columns(file_path)
BINARY FILE:
Countrec() : to read contents of file “passenger.dat” and display the details of those passengers
where amount is above 5000.
import struct
def write_record(file_name):
try:
file.write(record)
except FileNotFoundError:
def display_records(file_name):
try:
while True:
record = file.read(record_size)
if not record:
break
passenger_number, name, amount = struct.unpack('i 10s f', record)
except FileNotFoundError:
def count_records_above_5000(file_name):
count = 0
try:
while True:
record = file.read(record_size)
if not record:
break
count += 1
except FileNotFoundError:
# Menu-driven program
def menu():
file_name = 'passenger.dat'
while True:
print("\nMenu:")
print("4. Exit")
if choice == '1':
write_record(file_name)
display_records(file_name)
count_records_above_5000(file_name)
break
else:
menu()
2.Create a binary file with roll number, name and marks. Input a roll number and update the marks.
import struct
# Function to create a binary file with roll number, name, and marks
def create_file(file_name):
try:
records = [
except FileNotFoundError:
try:
while True:
record = file.read(record_size)
if not record:
break
if roll == roll_number:
file.seek(-struct.calcsize('i f'), 1)
break
else:
except FileNotFoundError:
file_path = 'student_data.bin'
create_file(file_path)
roll_number_to_update = 102
new_marks_value = 75
14. Create a CSV file user.csv .Entering user-id and password, read and search the password for given
user id.
A CSV file contains data Empno, Employee Name, Salary. Write a program to implement functions:
count1() : to count total number of employees getting salary more than 10000
count2() : to display names of students who get salary between 5000 to 9000
import struct
# Function to create a binary file with roll number, name, and marks
def create_file(file_name):
try:
records = [
except FileNotFoundError:
try:
while True:
record = file.read(record_size)
if not record:
break
if roll == roll_number:
file.seek(-struct.calcsize('i f'), 1)
break
else:
except FileNotFoundError:
file_path = 'student_data.bin'
create_file(file_path)
roll_number_to_update = 102
new_marks_value = 75
STACK
i) Item No.
ii) Name
Write a menu driven program to implement Push (), Pop () and Display () .
import csv
def write_records(file_name):
writer = csv.writer(file)
records = [
writer.writerows(records)
def read_records(file_name):
reader = csv.DictReader(file)
print(row)
def count_employees_above_10000(file_name):
count = 0
count += 1
def display_names_salary_between_5000_and_9000(file_name):
reader = csv.DictReader(file)
salary = int(row['Salary'])
def average_salary(file_name):
total_salary = 0
count = 0
reader = csv.DictReader(file)
total_salary += int(row['Salary'])
count += 1
# Example usage
file_path = 'employee_data.csv'
read_records(file_path)
count_employees_above_10000(file_path)
display_names_salary_between_5000_and_9000(file_path)
average_salary(file_path)
• Create table table CUSTOMER with following data and frame queries for the following:
a. To list the names of customer with their date of joining in descending order.
e. Describe CUSTOMER
Connections:
USE session2023;
CUST_NAME VARCHAR(50),
ACCT_TYPE VARCHAR(20),
ACCUMULATED_AMT INT,
DOJ DATE,
GENDER CHAR(1)
);
FROM CUSTOMER_DETAILS
ORDER BY DOJ DESC;.
b. SELECT *
FROM CUSTOMER_DETAILS
c. SELECT *
FROM CUSTOMER_DETAILS
D. DESCRIBE CUSTOMER_DETAILS;