C++ program to find largest element in an array

Finding largest element in an array in C++

After going through this article you can code a C++ program to find the largest element in an array. Finding largest element in an array is not a complicated process , we just had to iterate through the whole array and check if for the largest element present in it. There are steps and algorithm below that will help you in the process.

Largest element in an array.

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

We can find largest element in an array by following these steps:-

  1. Initialize the required variables.
  2. Accept the inputs from user.
  3. Assume largest element is present in first position.
  4. Iterate each element through a for loop.
  5. Check if element iterated is greater than large.
  6. If it is greater than large, change the value of large with that element.
  7. Print large as it stores the largest element of array.
C++ program to find largest element in an array

Algorithm to find largest element in an array

  • Start
  • Large = arr[0]
  • For i= 0 to n-1
  • If Large<arr[i]
  • Large= arr[i]
  • Exit

C++ program to find largest element in an array

#include <iostream>
using namespace std;

int main()
{
    int i, n, large=0;
    int arr[100];

    cout << "Enter the size of array: ";
    cin >> n;

    cout<<"Enter the elements of array: "<<endl;
    for(i = 0; i < n; ++i)
    {
       cin >> arr[i];
    }
    large= arr[0];//Assuming first element of the array is largest
    //Iterating each element to find the largest.
    for(i = 0;i < n; ++i)
    {
       //Finding largest element
       if(large < arr[i])
           large = arr[i];
    }
    cout << "Largest element in the array = " << large;

    return 0;
}
Output:
Enter the size of array: 5
Enter the elements of array: 
1
14
3
7
0
Largest element in the array = 14