Q. Write a program to find largest element in an array.

Solution :- In this program, we will find the largest/Maximum value element in an array.

For Example :-
input : arr[]={ 8, 9, 1, 2, 4 }
output : 9

So as we can see that the largest element in arr[] = {8, 9, 1, 2, 4 } is 9 so the output is 9.

Largest Number Algorithm

START
Step 1 → Take an array Arr and define its values
Step 2 → Declare one variable (VAR) and assign 1st element of array to it
Step 3 → Loop for each value of Arr Repeat Step 4
Step 4 → If Arr[I] > VAR, Assign Arr[I] to VAR
Step 5 → After loop finishes, Display VAR which holds the largest element of array
STOP


Largest Number Program

  • C
  • C++
  • Java
  • Python
  • C#
  • PHP
  • #include <stdio.h>
    int main() {
    int i, arr[5]={ 7, 9, 1, -15, 0};
    int VAR = arr[0];
    for (i = 1; i < 5; ++i) {
    if (VAR < arr[i])
    VAR = arr[i];
    }
    printf("Largest element in an Array is = %d", VAR);
    return 0;
    }
    
    #include <iostream>
    using namespace std;
    int main()
    {
    int i, arr[5]={ 7, 9, 1, -15, 0};
    int VAR = arr[0];
    for(i = 1;i < 5; ++i)
    {
    if(VAR < arr[i])
    VAR = arr[i];
    }
    cout << "Largest element in an Array is = " << VAR;
    return 0;
    }
    
    public class LFC {
    public static void main(String[] args) {
    int[] numArray = { 7, 9, 1, -15, 0};
    int largest = numArray[0];
    for (int num: numArray) {
    if(largest < num)
    largest = num;
    }
    System.out.format("Largest element in an Array is = %d", largest);
    }
    }
    
    number = [7, 9, 1, -15, 0]
    largest_number = max(number);
    print("Largest element in an Array is = ", largest_number)
    
    using System;
    class LFC
    {
    static void Main()
    {
    int i       = 0;
    int large   = 0;
    //array declaration
    int[] arr = {7, 9, 1, -15, 0};
    //reading array elements 
    //assigning first element to the array
    large = arr[0];
    //loop to compare value of large with other elements
    for (i = 1; i < 5; i++)
    {
    //if large is smaller than other element
    //assig that element to the large
    if (large < arr[i])
    large = arr[i];
    }
    //finally, we will have largest element, printing here
    Console.WriteLine("Largest element in an Array = " + large);
    }
    }
    
    // Returns maximum in array 
    function getMax($arr)  
    { 
    $n = count($arr);  
    $max = $arr[0]; 
    for ($i = 1; $i < $n; $i++)  
    if ($max < $arr[$i]) 
    $max = $arr[$i]; 
    return $max;        
    } 
    // Driver code 
    $arr = array(7, 9, 1, -15, 0); 
    echo "Largest element in an Array : ";
    echo(getMax($arr)); 
    
    #include <stdio.h>
    int main() {
    int i, arr[5]={ 7, 9, 1, -15, 0};
    int VAR = arr[0];
    for (i = 1; i < 5; ++i) {
    if (VAR < arr[i])
    VAR = arr[i];
    }
    printf("Largest element in an Array is = %d", VAR);
    return 0;
    }
    

    Output

    Largest element in an Array is = 9
    

    Recommended Programs

       Program to find smallest element in an array.
       Program to check whether given number is prime or not