C++ program to find largest element in an array

In this article, you will learn a C++ program to find the largest element in an array. The user will be asked to enter the total value “n” for array elements. For example, if the user enters 5. The program takes 5 array elements from the user and stores them in the following way.

largest element in an array cpp program

Example: Find the largest element in an array

//C++ program to find largest element in an array
#include <iostream>
using namespace std;
int main(){
   int n, largest;
   int ary[50];
   cout<<"How many array element you want to enter? ";
   cin>>n;

   for(int i = 0; i < n; i++) {
      cout<<"Element "<<(i+1)<< ": ";
      cin>>ary[i];
   }
   largest = ary[0];
   for(int i = 1;i < n; i++) {
     
      if(largest < ary[i])
         largest = ary[i];
   } 
   cout<<"Largest element is: "<<largest;
   return 0;
}

Output

cpp program to find largest element in an array

Recommended Articles

C++ program to interchange two numbers

C++ student grade program

C++ program to swap two numbers