[go: up one dir, main page]

0% found this document useful (0 votes)
3 views8 pages

L11

The document outlines a series of programming assignments that involve creating modules and packages for mathematical operations, geometry calculations, and student management. It includes specific tasks such as implementing addition, subtraction, multiplication, division, and finding the majority element in a list, as well as creating a menu-driven interface for managing student details. Each assignment provides example input and expected output to guide the implementation.
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)
3 views8 pages

L11

The document outlines a series of programming assignments that involve creating modules and packages for mathematical operations, geometry calculations, and student management. It includes specific tasks such as implementing addition, subtraction, multiplication, division, and finding the majority element in a list, as well as creating a menu-driven interface for managing student details. Each assignment provides example input and expected output to guide the implementation.
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/ 8

LAB ASSIGNMENT 11

Q1. Create a module math_operations.py with functions for:

I. Addition

II. Subtraction

III. Multiplication

IV. Division

Write a program to import this module and perform these operations on user-provided numbers.

Implement a function to find the majority element in a list. [Do not use any inbuilt function].

import math_operations as m

a = float(input("Enter the 1st number:"))

b = float(input("enter the 2nd number:"))

print(m.Addition(a,b))

print(m.Subtraction(a,b))

print(m.Multiplication(a,b))

print(m.Division(a,b))

def find_majority_element(nums):

candidate = None

count = 0

for num in nums:

if count == 0:

candidate = num

if num == candidate:

count += 1
else:

count -= 1

count = 0

for num in nums:

if num == candidate:

count += 1

if count > len(nums) // 2:

return candidate

else:

return "No majority element found"

nums = [3, 3, 4, 2, 3, 3, 2, 3]

majority_element = find_majority_element(nums)

print(f"The majority element is: {majority_element}")

INPUT – a = 2

b=2

Q2. Create a package geometry with:

I. circle.py containing functions to calculate the area and circumference of a circle.

II. rectangle.py containing functions to calculate the area and perimeter of a rectangle.

Write a program to import and use the package.

from geometry import circle


a = float(input("Enter the radius:"))

circle.area(a)

circle.circumference(a)

from geometry import rectangle

l = float(input("Enter the lenght:"))

b = float(input("Enter the breadth:"))

rectangle.area(l,b)

rectangle.perimeter(l,b)

INPUT –

a=2

l=2

b=2

OUTPUT –

Q3 Write a program that:

I. Finds the factorial, square root, and greatest common divisor (GCD) of numbers using

the math module.

II. Accepts input from the user for these operations.

import math

num_factorial = int(input("Enter a number to find its factorial: "))


factorial_result = math.factorial(num_factorial)

print(f"The factorial of {num_factorial} is {factorial_result}")

num_sqrt = float(input("Enter a number to find its square root: "))

sqrt_result = math.sqrt(num_sqrt)

print(f"The square root of {num_sqrt} is {sqrt_result}")

num_gcd1 = int(input("Enter the first number to find the GCD: "))

num_gcd2 = int(input("Enter the second number to find the GCD: "))

gcd_result = math.gcd(num_gcd1, num_gcd2)

print(f"The GCD of {num_gcd1} and {num_gcd2} is {gcd_result}")

INPUT –

num_factorial = 4

num_sqrt = 4

num_gcd1 = 4

num_gcd2 = 2

OUTPUT –

Q4. Create a package math_operations with the following structure

math_operations/

├── __init__.py

├── basic/

│ ├── addition.py
│ ├── subtraction.py

├── advanced/

├── factorial.py

├── power.py

Write a program to demonstrate importing functions from both basic and advanced sub-

packages.

from math_operations.basic.addition import add

from math_operations.basic.subtraction import subtract

from math_operations.advanced.factorial import factorial

from math_operations.advanced.power import power

def main():

a, b = 10, 5

print(f"Addition of {a} and {b}: {add(a, b)}")

print(f"Subtraction of {a} and {b}: {subtract(a, b)}")

n=5

base, exponent = 2, 3

print(f"Factorial of {n}: {factorial(n)}")

print(f"{base} raised to the power of {exponent}: {power(base, exponent)}")

if __name__ == "__main__":

main()

OUTPUT –
Q5. Create a package student_management with:

I. add_student.py to add student details (name, roll, marks).

II. update_marks.py to update marks for a specific student.

III. view_students.py to view all student details.

Write a main script that provides a menu-driven interface to use the package.

from student_management.add_student import add_student

from student_management.update_marks import update_marks

from student_management.view_student import view_students

def main():

while True:

print("\nStudent Management System")

print("1. Add Student")

print("2. Update Marks")

print("3. View Students")

print("4. Exit")

choice = input("Enter your choice: ")

if choice == '1':

name = input("Enter student name: ")

roll = input("Enter student roll number: ")

marks = float(input("Enter student marks: "))


add_student(name, roll, marks)

print("Student added successfully!")

elif choice == '2':

roll = input("Enter student roll number: ")

new_marks = float(input("Enter new marks: "))

if update_marks(roll, new_marks):

print("Marks updated successfully!")

else:

print("Student not found!")

elif choice == '3':

print("Student Details:")

view_students()

elif choice == '4':

print("Exiting the system.")

break

else:

print("Invalid choice. Please try again.")

if __name__ == "__main__":

main()

OUTPUT –

You might also like