0% found this document useful (0 votes)
9 views9 pages

Assigment 4

Uploaded by

arcanedishidishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views9 pages

Assigment 4

Uploaded by

arcanedishidishi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Assigment 4

Practical 1:
MyUdm.py:
import math

def Interest(principal, time, rate):

simple_interest = (principal * time * rate) / 100

compound_interest = principal * ((1 + rate / 100) ** time) - principal

print("Simple Interest" ,simple_interest)

print("Compound Interest" .compound_interest)

def MaxNo(a, b, c):

return max(a, b, c)

def Area(radius):

return math.pi * radius ** 2

def Factorial(n):

if n < 0:

return "Factorial not defined for negative numbers"

elif n == 0 or n == 1:

return 1

else:

factorial = 1

for i in range(2, n + 1):

factorial *= i

return factorial

Code:

import MyUDM

def main():

while True:

print("\n--- Menu ---")

print("1. Calculate Interest")

print("2. Find Maximum of Three Numbers")

print("3. Calculate Area of a Circle")

print("4. Calculate Factorial")

print("5. Exit")

choice = int(input("Enter your choice: "))


if choice == 1:

principal =int(input("Enter principal amount: "))

time =int(input("Enter time in years: "))

rate = int(input("Enter rate of interest: "))

MyUDM.Interest(principal, time, rate)

elif choice == 2:

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

b = int(input("Enter second number: "))

c = int(input("Enter third number: "))

max_number = MyUDM.MaxNo(a, b, c)

print("The maximum number is",max_number)

elif choice == 3:

radius = float(input("Enter the radius of the circle: "))

Area = MyUDM.Area(radius)

print("The Area of the circle is", Area)

elif choice == 4:

number = int(input("Enter a number: "))

factorial = MyUDM.Factorial(number)

print("The factorial of number is",factorial)

elif choice == 5:

print("Exiting the program.")

break

else:

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

main()

Output:

--- Menu ---

1. Calculate Interest

2. Find Maximum of Three Numbers

3. Calculate Area of a Circle


4. Calculate Factorial

5. Exit

Enter your choice: 1

Enter principal amount: 1200

Enter time in years: 2

Enter rate of interest: 2

Simple Interest 48.0

Compound Interest 48.48000000000002

--- Menu ---

1. Calculate Interest

2. Find Maximum of Three Numbers

3. Calculate Area of a Circle

4. Calculate Factorial

5. Exit

Enter your choice: 2

Enter first number: 4

Enter second number: 2

Enter third number: 2

The maximum number is 4

--- Menu ---

1. Calculate Interest

2. Find Maximum of Three Numbers

3. Calculate Area of a Circle

4. Calculate Factorial

5. Exit

Enter your choice: 3

Enter the radius of the circle: 16

The Area of the circle is 804.247719318987

--- Menu ---

1. Calculate Interest

2. Find Maximum of Three Numbers

3. Calculate Area of a Circle

4. Calculate Factorial

5. Exit

Enter your choice: 4

Enter a number: 2

The factorial of number is 2

Practical 2
StrLst.py

def ShortenString(s, n):

if len(s) > n:

return s[:n]

return s

def DeleteChar(s, char):

return s.replace(char, '')

def ShiftRight(lst):

if len(lst) == 0:

return lst

return [lst[-1]] + lst[:-1]

def ShiftLeft(lst):

if len(lst) == 0:

return lst

return lst[1:] + [lst[0]]

Code:

import StrLst

def main():

while True:

print("\n--- Menu ---")

print("1. Shorten String")

print("2. Delete Character from String")

print("3. Shift List Right")

print("4. Shift List Left")

print("5. Exit")

choice = int(input("Enter your choice: "))

if choice == 1:

s = input("Enter the string: ")

n = int(input("Enter the number of characters to shorten to: "))

result = StrLst.ShortenString(s, n)

print("Shortened String",result)

elif choice == 2:

s = input("Enter the string: ")

char = input("Enter the character to delete: ")

result = StrLst.DeleteChar(s, char)


print("Modified String",result)

elif choice == 3:

lst = input("Enter the list elements separated by commas: ").split(',')

result = StrLst.ShiftRight(lst)

print("List after Shift Right",result)

elif choice == 4:

lst = input("Enter the list elements separated by commas: ").split(',')

result = StrLst.ShiftLeft(lst)

print("List after Shift Left",result)

elif choice == 5:

print("Exiting the program.")

break

else:

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

main()

Output:

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 1

Enter the string: "24"

Enter the number of characters to shorten to: 2

Shortened String "2

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 1

Enter the string: "I love Python"


Enter the number of characters to shorten to: 3

Shortened String "I

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 2

Enter the string: "Everything is easy when you love what you are doing"

Enter the character to delete: you love

Modified String "Everything is easy when what you are doing"

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 3

Enter the list elements separated by commas: "consistency","is","the","key"

List after Shift Right ['"key"', '"consistency"', '"is"', '"the"']

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 4

Enter the list elements separated by commas: "god","makes","everything","possible"

List after Shift Left ['"makes"', '"everything"', '"possible"', '"god"']

--- Menu ---

1. Shorten String

2. Delete Character from String

3. Shift List Right

4. Shift List Left

5. Exit

Enter your choice: 5

Exiting the program.


--- Menu ---

1. Calculate Interest

2. Find Maximum of Three Numbers

3. Calculate Area of a Circle

4. Calculate Factorial

5. Exit

Enter your choice: 5

Exiting the program.

You might also like