Program to find largest element in an array
CODE
Program to find largest
element in an array
Given an array, find the largest element in it.
// C++ program to find maximum
// in arr[] of size n
#include <bits/stdc++.h>
using namespace std;
int largest(int arr[], int n)
{
int i;
// Initialize maximum
element
int max =
arr[0];
// Traverse array
elements
// from second and
compare
// every element with
current max
for (i = 1; i
< n; i++)
if
(arr[i] > max)
max =
arr[i];
return max;
}
// Driver Code
int main()
{
int arr[] =
{19, 34, 475, 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
Example:
Input : arr[] = {50, 20, 4}
Output : 50
Input : arr[] = {20, 900, 20, 4, 100}
Output : 900
Program to find largest element in an array
Reviewed by Unknown
on
September 09, 2018
Rating:
No comments:
If you have any doubt or query ,comment below: