Python Program to find maximum element in an array

Program

number_array = list()
size = int(input("Enter the size of array:\t"))
print("Enter the numbers in array: ")
for i in range(int(size)):
    n = input()
    number_array.append(int(n))
max = number_array[0]
location = int(0)
index = int(1)
for index in range(len(number_array)):
    if number_array[index] > max:
        max = number_array[index]
        location = index
print("Maximum element in the array is: ", max)
print("Position is ", location + 1)

This program finds the largest element in an array and its position.

  1. User Input for Array Size

    • The user enters the size of the array.
  2. Array Input

    • A loop runs for size times to take integer inputs and store them in number_array.
  3. Find Maximum Element

    • The first element of the array is initially set as max.
    • A loop iterates through the array, updating max whenever a larger value is found.
    • The location variable stores the index of the maximum value.
  4. Display the Maximum Value and Its Position

    • The maximum value is printed.
    • The position is displayed as location + 1 (since arrays use zero-based indexing).

Key Takeaways

  • Uses a loop to find the largest number in an array.
  • Keeps track of the maximum value and its position.
  • Displays the position in a user-friendly way (1-based index).

Output

$ python3 maximum_element_in_array.py
Enter the size of array:        5
Enter the numbers in array:
234
899
45
345
678
Maximum element in the array is:  899
Position is  2