Aim:
To find the maximum of a list of numbers.
Algorithm:
• Start the program.
• Define a function max_of_list that takes a list as input.
• Initialize a variable max_num with the first element of the list.
• Loop through the list and compare each element with max_num.
• If an element is greater than max_num, update max_num.
• Return max_num as the maximum value in the list.
Code:
def max_of_list(numbers):
max_num = numbers[0]
for num in numbers:
if num > max_num:
max_num = num
return max_num
# Example usage
print(max_of_list([4, 7, 1, 12, 9]))
Sample Output:
12
Viva Questions:
• What is the purpose of this program?
• How does the loop help in finding the maximum number?
• Can this program handle an empty list? Why or why not?
• What would you modify to find the minimum value instead?
• How does this approach compare to using Python’s built-in max() function?
Result:
The program successfully finds the maximum number in a list.