Q:

Program to print the largest element present in an array

belongs to collection: Array Programs

0

Explanation

In this program, we need to find out the largest element present in the array and display it. This can be accomplished by looping through the array from start to end by comparing max with all the elements of an array. If any of element is greater than max, then store a value of the element in max. Initially, max will hold the value of the first element. At the end of the loop, max represents the largest element in the array.

In the above array, initially, max will hold the value 25. In the 1st iteration, max will be compared with 11, since 11 is less than max. Max will retain its value. In the next iteration, it will be compared to 7, 7 is also less than max, no change will be made to the max. Now, max will be compared to 75. 75 is greater than max so that max will hold the value of 75. Continue this process until the end of the array is reached. At the end of the loop, max will hold the largest element in the array.

Algorithm

  1. Declare and initialize an array.
  2. Store first element in variable max.
  3. Loop through the array from 0 to length of the array and compare the value of max with elements of the array.
  4. If any element is greater than max, max will hold the value of that element.
  5. At last, max will hold the largest element in the array.

Input:

arr = [25, 11, 7, 75, 56]  

Output:

Largest element present in given array: 75

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python

#Initialize array   
arr = [25, 11, 7, 75, 56];   
   
#Initialize max with first element of array.  
max = arr[0];  
   
#Loop through the array  
for i in range(0, len(arr)):  
    #Compare elements of array with max  
   if(arr[i] > max):  
       max = arr[i];  
         
print("Largest element present in given array: " + str(max));  

 

Output:

Largest element present in given array: 75

 

C

#include <stdio.h>  
   
int main()  
{  
    //Initialize array   
    int arr[] = {25, 11, 7, 75, 56};    
      
    //Calculate length of array arr  
    int length = sizeof(arr)/sizeof(arr[0]);  
      
    //Initialize max with first element of array.  
    int max = arr[0];  
      
    //Loop through the array  
    for (int i = 0; i < length; i++) {   
        //Compare elements of array with max  
       if(arr[i] > max)  
           max = arr[i];  
    }    
    printf("Largest element present in given array: %d\n", max);  
    return 0;  
}  

 

Output:

Largest element present in given array: 75

 

JAVA

public class LargestElement {  
    public static void main(String[] args) {      
          
        //Initialize array   
        int [] arr = new int [] {25, 11, 7, 75, 56};   
          
        //Initialize max with first element of array.  
        int max = arr[0];  
          
        //Loop through the array  
        for (int i = 0; i < arr.length; i++) {   
            //Compare elements of array with max  
           if(arr[i] > max)  
               max = arr[i];  
        }    
        System.out.println("Largest element present in given array: " + max);  
    }  
}  

 

Output:

Largest element present in given array: 75

 

C#

using System;  
                      
public class LargestElement  
{  
    public static void Main()  
    {  
        //Initialize array   
        int [] arr = new int [] {25, 11, 7, 75, 56};   
          
        //Initialize max with first element of array.  
        int max = arr[0];  
          
        //Loop through the array  
        for (int i = 0; i < arr.Length; i++) {   
            //Compare elements of array with max  
           if(arr[i] > max)  
               max = arr[i];  
        }    
        Console.WriteLine("Largest element present in given array: " + max);  
    }  
}  

 

Output:

Largest element present in given array: 75

 

PHP

<!DOCTYPE html>  
<html>  
<body>  
<?php  
//Initialize array   
$arr = array(25, 11, 7, 75, 56);   
   
//Initialize max with first element of array.  
$max = $arr[0];  
   
//Loop through the array  
for ($i = 0; $i < count($arr); $i++) {   
    //Compare elements of array with max  
   if($arr[$i] > $max)  
       $max = $arr[$i];  
}    
print("Largest element present in given array: " . $max);  
?>  
</body>  
</html>  

 

Output:

Largest element present in given array: 75

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Program to print the number of elements present in... >>
<< Program to print the elements of an array present ...