Table of Contents
Introduction
In this section of python programming, we will understand the code to find the largest element in the given array.
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
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.
0 Comments