Program to find the largest element in array using Python

by | Jan 26, 2021 | Python Programs

Home » Python » Python Programs » Program to find the largest element in array using Python

Introduction

In this section of python programming, we will understand the code to find the largest element in the given array.

Program to find the largest element in array using Python

Program

def max(arr,length):

    max_ele = arr[0]

# Traverse elements from index 1

    for i in range(1,length):

# Comparing each element

        if arr[i] > max_ele:

            max_ele = arr[i]

    return max_ele




arr = [2, 5, 4, 3, 10]

length = len(arr)

result = max(arr,length)

print("The largest number in given array is ",result)

Output

Program to find the largest element in array using Python Output

Explanation

In the above python code we have created an array with the given elements and a function max() which will traverse each and every element in the array and compare it with the current max and returns the maximum element.

Author

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author