BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Find largest element in an array

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: C++ Programs

This program finds the largest element in an array. User is asked to enter the value of n(number of elements in array) then program asks the user to enter the elements of array. The program then finds the largest element and displays it.

To understand this program you should have the basic knowledge of loops, array and C++ if-else statement.

Example: A Program to find the largest element in an array of n elements

#include <iostream>
using namespace std;
int main(){
   //n is the number of elements in the array
   int n, largest;
   int num[50];
   cout<<"Enter number of elements you want to enter: ";
   cin>>n;
   
   /* Loop runs from o to n, in such a way that first
    * element entered by user is stored in num[0], second 
    * in num[1] and so on. 
    */
   for(int i = 0; i < n; i++) {
      cout<<"Enter Element "<<(i+1)<< ": ";
      cin>>num[i];
   }
   // Storing first array element in "largest" variable
   largest = num[0];
   for(int i = 1;i < n; i++) {
      /* We are comparing largest variable with every element
       * of array. If there is an element which is greater than
       * largest variable value then we are copying that variable
       * to largest, this way we have the largest element copied
       * to the variable named "largest" at the end of the loop 
       *
       */
      if(largest < num[i])
         largest = num[i];
   } 
   cout<<"Largest element in array is: "<<largest;
   return 0;
}

Output:

Enter number of elements you want to enter: 5
Enter Element 1: 19
Enter Element 2: 21
Enter Element 3: 3
Enter Element 4: 89
Enter Element 5: 13
Largest element in array is: 89

Explanation:
User enters 5 which means the loop that stores the input values into the array runs 5 times, first entered value is stored in num[0], second in num[1] and so on.

The first element is assigned to the integer variable “largest”, this means that before starting of the second loop the value of largest is 19.

First Iteration of second loop: 19, which is the value of “largest” variable is compared with 21, since 21 is greater than 19, the “if” condition is true and the value of largest is now 21.

Second Iteration of loop: 21 (the value of largest) is compared with 3, since three is less than 21, nothing happens as the “if” condition is false.

Third Iteration of loop: 21 is compared with 89, 89 is greater than 21, “if” condition is true, the value of 89 is assigned to the “largest” variable.

Fourth Iteration of loop: 89(value of largest variable) is compared with 13, 13 is small so nothing happens.
Loop ended.

The value in “largest” variable is 89 at the end of loop, which we have displayed as the answer.

Try these related programs

  • C++ Program to find the smallest element in an array
  • C++ Program to find second smallest element in an array
  • C++ Program to find missing number in a sequential array

Top Related Articles:

  1. C++ Program to Find Second Smallest Element in an Array
  2. C++ Program to find the Missing Number
  3. C++ Program to add two numbers
  4. C++ Program to Find Smallest Element in an Array
  5. C++ Program to Find and display the Transpose of a Matrix

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap