0% found this document useful (0 votes)
4 views3 pages

Functions Assignment

The document outlines six programming tasks involving functions in Python. These tasks include converting meters to feet, calculating the area and perimeter of a rectangle, finding the maximum of three numbers, calculating a discounted price with a default parameter, identifying prime numbers in a range, and implementing a recursive function to sum the digits of a number. Each task includes specific requirements and sample outputs for guidance.

Uploaded by

aleezayf588
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)
4 views3 pages

Functions Assignment

The document outlines six programming tasks involving functions in Python. These tasks include converting meters to feet, calculating the area and perimeter of a rectangle, finding the maximum of three numbers, calculating a discounted price with a default parameter, identifying prime numbers in a range, and implementing a recursive function to sum the digits of a number. Each task includes specific requirements and sample outputs for guidance.

Uploaded by

aleezayf588
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

Question 1: Function with One Parameter

Write a function called convert_to_feet() that:

• Takes meters as a parameter

• Converts meters to feet (1 meter = 3.28084 feet)

• Returns the converted value

• Display the result with 2 decimal places

Sample Output:

Enter meters: 10

10.00 meters = 32.81 feet

Question 2: Function with Two Parameters

Write a function called calculate_rectangle() that:

• Takes length and width as parameters

• Returns both area and perimeter

• Call the function and display both values

Formulas:

• Area = length × width

• Perimeter = 2 × (length + width)

Sample Output:

Length: 5

Width: 3

Area = 15, Perimeter = 16


Question 3: Function with Conditional Logic

Write a function called find_maximum() that:

• Takes three numbers as parameters

• Returns the largest number

• Do NOT use built-in max() function (use if-else logic)

Sample Output:

Enter first number: 25

Enter second number: 42

Enter third number: 18

Maximum number is: 42

Question 4: Function with Default Parameter

Write a function called calculate_discount() that:

• Takes price and discount percentage as parameters

• Discount percentage should have a default value of 10%

• Returns the final price after discount

• Test it with and without providing the discount

Formula: Final Price = Price - (Price × Discount / 100)

Sample Output:

Price: 1000

With 10% discount (default): Final price = 900.00

With 20% discount: Final price = 800.00


Question 5: Function Calling Another Function

Write TWO functions:

1. is_prime(n) - checks if a number is prime (returns True/False)

2. print_primes_in_range(start, end) - prints all prime numbers between start and end
(calls is_prime function)

Sample Output:

Enter start: 10

Enter end: 50

Prime numbers between 10 and 50:

11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47

Q.6 Write a recursive function called sum_of_digits(n) that:

• Takes a positive integer as input

• Returns the sum of all digits using recursion (not loops)

• Also write a main program to test it

Sample Output:

Enter a number: 12345

Sum of digits of 12345 is: 15

Hint: Base case: if n < 10, return n. Recursive case: return last digit + sum_of_digits(remaining
number)

You might also like