View All C Programs

 Previews Example  View All C Programs Next Example 

Program to Find Largest Element of an Array in C

This program takes n number of element from user(where, n is specified by user) and stores data in an array. Then, this program displays the largest element of that array using loops.
#include <stdio.h>
#include <conio.h>
 main(){
    int i,n;
    int arr[100];
    printf("Enter total number of elements: ");
    scanf("%d",&n);
    printf("\n");
    for(i=0;i<n;++i)  /* Stores number entered by user. */
    {
       printf("Enter Number %d: ",i+1);
       scanf("%d",&arr[i]);
    }
    for(i=1;i<n;++i)  /* Loop to store largest number to arr[0] */
    {
       if(arr[0]<arr[i]) /* Change < to > if you want to find smallest element*/
           arr[0]=arr[i];
    }
    printf("Largest element = %d",arr[0]);
    getch();
}
Result
Enter total number of elements: 5
Enter Number 1: 10
Enter Number 2: 5
Enter Number 3: 15
Enter Number 4: 20
Enter Number 5: 50

Largest element = 50