Monday 5 January 2015

Cpp Program to find the Largest Element in an array


This is a Cpp Program which finds the largest element in an array.

Here initially first element in array and  its next element are compared array[0] and array[1] , if array[1] is greater then the value is stored in array[0] and now the second element is increased 2 .3 4 .. until the last element is reached.

PROGRAM :
#include <iostream>

using namespace std;

int main()
{
    int n, array[100],i,sum=0;

    cout << "Enter how many numbers ? :\t" ;
    cin >> n;
    cout << endl;

    for(i=0;i<n;i++)
    {
        cout << "Enter number " << i+1 << " : \t";
        cin >> array[i];
    }

    for(i=0;i<n;i++)
    {
        if(array[0] < array[i])
            array[0] = array[i];
    }

    cout << "The largest among the numbers is : \t" << array[0] << endl;

    return 0;
}

OUTPUT :


Cpp Programming - Largest element in an array