NCAA NIL Policy

Function Demonstration Programs

Function Demonstration Programs

Q1. Write a program that asks the user to enter a distance in kilometers, and then converts that distance to miles. The conversion formula is as follows:

Function Demonstration Programs

Miles Kilometer x 0.6214

Submission for Q1:

1. A Python code file

2. Screenshot of the Running program

Q2. Write the following codes

Submission of Q2 (All parts a, b, and c):

1. A summary of the program, what does program do

2. Screenshot of the Running program

a). def message():

print(‘I am Arthur,’)

print(‘King of the Britons.’)

message()

b) def main():

print(‘I have a message for you.’)

message()

print(‘Goodbye!’)

def message():

print(‘I am Arthur,’)

print(‘King of the Britons.’)

main()

c)

def area(width, length):

return width * length

def perimeter(width, length):

return 2 * (width + length)

def main():

width = float(input(“Enter the rectangle’s width: “))

length = float(input(“Enter the rectangle’s length: “))

print(‘The area is’, area(width, length))

print(‘The perimeter is’, perimeter(width, length))

main()

Solution

Q1: Distance Conversion Program

This program converts a distance from kilometers to miles using the formula:

Miles=Kilometers×0.6214\text{Miles} = \text{Kilometers} \times 0.6214

Here’s the Python code for Q1: Function Demonstration Programs

python
def km_to_miles(kilometers): # Conversion factor miles = kilometers * 0.6214 return miles def main(): # Ask the user to enter the distance in kilometers kilometers = float(input(“Enter distance in kilometers: “)) # Convert the distance to miles miles = km_to_miles(kilometers) # Display the result print(f”{kilometers} kilometers is equal to {miles:.2f} miles.”) # Call the main function to run the program main()…..

Q1: Distance Conversion Program

This program converts a distance from kilometers to miles using the formula:

Miles=Kilometers×0.6214\text{Miles} = \text{Kilometers} \times 0.6214

Here’s the Python code for Q1: Function Demonstration Programs

python
def km_to_miles(kilometers): # Conversion factor miles = kilometers * 0.6214 return miles def main(): # Ask the user to enter the distance in kilometers kilometers = float(input(“Enter distance in kilometers: “)) # Convert the distance to miles miles = km_to_miles(kilometers) # Display the result print(f”{kilometers} kilometers is equal to {miles:.2f} miles.”) # Call the main function to run the program main()…..