Program to get the largest element of an array using the function.

Here, we have a basic program example to print the largest element of the array using different languages. This program is created in c language, c++, Java, and Python.

Function to get the largest array element in C language

#include<stdio.h>
#define MAX 100
int findMaxElem(int []);
int n;
int main()
{
    int arr[MAX],maxelem,i;
       printf(" Input the number of elements to be stored in the array :");
       scanf("%d",&n);
       printf(" Input %d elements in the array :\n",n);
       for(i=0;i<n;i++)
        {
	      printf(" element - %d : ",i);
	      scanf("%d",&arr[i]);
	    }
    maxelem=findMaxElem(arr);
    printf(" The largest element in the array is : %d\n\n",maxelem);
    return 0;
}
int findMaxElem(int arr[])
{
    int i=1,maxelem;
    maxelem=arr[0];
    while(i < n)
	{
      if(maxelem<arr[i])
           maxelem=arr[i];
      i++;
    }
    return maxelem;
}

Function to get the largest array element in C++ language

#include <iostream>
#define MAX 100
using namespace std;
int findMaxElem(int []);
int n;
int main(){
    int arr[MAX],maxelem,i;
       cout<<" Input the number of elements to be stored in the array :";
       cin>>n;
       cout<<" Input "<<n<<" elements in the array :\n";
       for(i=0;i<n;i++)
        {
	      cout<<" element - "<<i<<" : ";
	      cin>>arr[i];
	    }
    maxelem=findMaxElem(arr);
    cout<<" The largest element in the array is : "<<maxelem;
    return 0;
}
int findMaxElem(int arr[])
{
    int i=1,maxelem;
    maxelem=arr[0];
    while(i < n)
	{
      if(maxelem<arr[i])
           maxelem=arr[i];
      i++;
    }
    return maxelem;
}

Function to get the largest array element in Python language

def MaxElement(arr,n):
    maxVal = arr[0]
    for i in range(1,n):
        if arr[i] > maxVal:
            maxVal = arr[i]
    return maxVal
n = int(input("Enter the number of elements in the array: "))
arr = []
print("Enter ",n," elements of the array: ")
for i in range(n):
    numbers = int(input())
    arr.append(numbers)
maxVal = MaxElement(arr,n)
print("Largest element in the given array is: ",maxVal)

Function to get the largest array element in Java language

import java.util.Scanner;
import java.io.*;
import java.util.*;

public class maxElement
 {
   static void findLargest(int arr[], int n){
   for(int i=0;i<n;i++)
    {
      for(int j=i+1;j<n;j++)
       {
         if(arr[i]<arr[j])
          {
            int temp=arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
          }
       }
     }
   System.out.println("Largest Element is: "+arr[0]);
 }

 public static void main(String[] args)
  {
    Scanner sc=new Scanner(System.in);
    int n;
    System.out.println("Enter the size of the array");
     n=sc.nextInt();

     int arr[]=new int[n];
     System.out.println("Enter the array elements");
for(int i=0;i<n;i++)
 {
   arr[i]=sc.nextInt();
  }
  findLargest(arr,n);
  }
}