Find Largest Number using Array in C++


This is a C++ program that finds the largest element in an array of integers.

  • The program first asks the user to enter the number of elements they want to enter into the array. It then uses a for loop to prompt the user to enter each element one by one and stores them in the num array.
  • After filling up the array, the program initializes a variable largest to the first element of the array num[0]. It then loops through the remaining elements of the array using another for loop and compares each element with the current value of largest. If the element is greater than largest, the program updates the value of largest to that element.
  • Finally, the program outputs the value of largest as the largest element in the array.

Source Code

#include <iostream>
using namespace std;
int main()
{
  int n,largest;
  int num[50];
  cout<<"\nEnter number of elements you want to enter: ";
  cin>>n;
  for(int i=0;i<n;i++)
  {
    cout<<"\nEnter Element "<<(i+1)<<": ";
    cin>>num[i];
  }
  largest = num[0];
  for(int i=1;i<n;i++)
  {
    if(largest < num[i])
      largest = num[i];
  }
  cout<<"\nLargest element in array is: "<<largest;
  return 0;
}
To download raw file Click Here

Output

Enter number of elements you want to enter: 5
Enter Element 1: 11
Enter Element 2: 12
Enter Element 3: 13
Enter Element 4: 14
Enter Element 5: 15
Largest element in array is: 15

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples