Program to find largest element in an array in C++


In this tutorial, we will be discussing a program to find the largest element in an array.

For this, we will be provided with an array. Our task is to find the largest number from the elements inside the array.

Example

 Live Demo

#include <bits/stdc++.h>
using namespace std;
//finding largest integer
int largest(int arr[], int n){
   int i;
   int max = arr[0];
   //traversing other elements
   for (i = 1; i < n; i++)
   if (arr[i] > max)
      max = arr[i];
   return max;
}
int main(){
   int arr[] = {10, 324, 45, 90, 9808};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Largest in given array is " << largest(arr, n);
   return 0;
}

Output

Largest in given array is 9808

Updated on: 04-May-2020

298 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements