Greatest Elements in an array in C++ Programming


The program finds the greatest element in an array entered by the user.

  • The program starts by declaring an integer array a of size 100 and variables n (for the size of the array), i (for loop iteration), and t (to store the greatest element).
  • The user is then prompted to enter the size of the array, which is stored in the variable n. The program then uses a for loop to prompt the user to enter values for each element in the array, which are stored in the array a.
  • Next, the program sets the variable t to the first element of the array a (i.e., t=a[0]). It then uses another for loop to iterate over the remaining elements in the array a and compare each element with the current value of t. If an element is greater than t, then t is updated to that element.
  • Finally, the program prints the value of t, which is the greatest element in the array.

Overall, the program allows the user to enter any number of elements in an array and then finds and prints the greatest element in the array.

Source Code

#include<iostream>
using namespace std;
//Program to find Greatest element in an array.
int main()
{
    int a[100],n,i,t;
    cout<<"\nEnter The Limit : ";
    cin>>n;//3
    for(i=0; i<n; i++)
    {
        cout<<"\nEnter The Value  : ";
        cin>>a[i];
    }//10,20,30
    t=a[0];//10
    for(i=1; i<n; i++)
    {
        if(t<a[i])//10<20  20<30
        {
            t=a[i];//20  30
        }
    }
    cout<<"\nThe Greatest No is : "<<t;
    return 0;
}
 

Output

Enter The Limit : 5

Enter The Value  : 23

Enter The Value  : 12

Enter The Value  : 34

Enter The Value  : 2

Enter The Value  : 34

The Greatest No is : 34
To download raw file Click Here

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