0% found this document useful (0 votes)
10 views5 pages

Lab Report 05

python
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)
10 views5 pages

Lab Report 05

python
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/ 5

DEPARTMENT OF MECHANICAL ENGINEERING COLLEGE OF E&ME, NUST,

RAWALPINDI

Subject: Fundamental of programming

Lab Number # 05

SUBMITTED TO:

Instructor Name: Dr Asad Mansoor

Lab Engineer: Fatima Ismail

SUBMITTED BY:

Muhammad Talha

DE- 45 Syn A Mechanical Engineering

REG NO # 481402

SUBMITION DATE: 17 March 2024


Task 1: Question statement

Write a function that will convert miles to kilometers and kilometers to miles.
The user will indicate both a number (representing a distance) and a choice of whether that
number is in miles to be converted to kilometers or kilometers to be converted to miles.
1 kilometer = 0.621 miles, 1 mile = 1.61 kilometers.

CODE:

def conversion(distance, unit):


# Checking the unit for conversion
if unit.lower() == 'km' or unit.lower() == 'kilometer':
# Convert from kilometers to miles
converted = distance * 1.60934
return (converted, 'kilometers')
elif unit.lower() == 'mile' or unit.lower() == 'miles':
# Convert from miles to kilometers
converted = distance * 0.621371
return (converted, 'miles')
else:
# Invalid unit entered
print("Invalid entry!")
return None

def main():
# Taking input from the user
distance = float(input("Enter the distance you want to convert: "))
unit = input("In which unit do you want to convert (km/mile)? ").lower()

# Calling the conversion function


updated_distance = conversion(distance, unit)

# Displaying the converted distance if valid


if updated_distance is not None:
print("Converted distance is: ", updated_distance[0],
updated_distance[1])

# Calling the main function


main()

Output:
Task 2: Question statement

Write a function for calculating the factorial of a number.


The factorial is calculated as: 𝑛! = ෑ 𝑖=1 𝑛 𝑖
For example: 5! = 1*2*3*4*5 = 120
Then write two other functions for calculating:
Combinations: 𝑛 𝑟 = 𝑛! 𝑟!⋅ 𝑛−𝑟 !
Permutations: 𝑃𝑟 = 𝑛! 𝑛−𝑟 !

CODE:

# Taking input for n and r from the user


n = int(input("Enter the value of n: "))
r = int(input("Enter the value of r: "))

# Define factorial function


def factorial(num):
result = 1
for i in range(1, num + 1):
result *= i
return result

# Calculate factorial
factorial_n = factorial(n)
factorial_r = factorial(r)
factorial_n_minus_r = factorial(n - r)

# Calculate combinations
combinations = factorial_n // (factorial_r * factorial_n_minus_r)

# Calculate permutations
permutations = factorial_n // factorial_n_minus_r

# Printing the results


print("Factorial of", n, ":", factorial_n)
print("Combinations of choosing", r, "from", n, ":", combinations)
print("Permutations of choosing", r, "from", n, ":", permutations)
OUTPUTS:

Task 3: Question statement

Write a Python Function that takes 4 numbers as parameters and returns the sum of
biggest number and the smallest number.

CODE:

# Function to find the smallest among four numbers


def smallest(a, b, c, d):
if (a < b and a < c and a < d):
return a
elif (b < a and b < c and b < d):
return b
elif (c < a and c < b and c < d):
return c
elif (d < a and d < b and d < c):
return d

# Function to find the largest among four numbers


def largest(a, b, c, d):
if (a > b and a > c and a > d):
return a
elif (b > a and b > c and b > d):
return b
elif (c > a and c > b and c > d):
return c
elif (d > a and d > b and d > c):
return d

# Main function to interact with the user


def main():
# Taking input from the user
a = int(input("Enter value for 'a': "))
b = int(input("Enter value for 'b': "))
c = int(input("Enter value for 'c': "))
d = int(input("Enter value for 'd': "))

# Calculating sum of largest and smallest number


sum = largest(a, b, c, d) + smallest(a, b, c, d)

# Printing the result


print("Sum of largest and smallest number is", sum)

# Calling the main function


main()

OUTPUTS:

********************************************************************************

You might also like