Display Largest Element of an array

C Programming Language / Array in C Language

1389

Program:

 /*
Display Largest Element of an array
 Author: atnyla Developer
 */

  
#include <stdio.h>

int main()
{
    int i, n;
    float arr[100];

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &n);
    printf("\n");

    // Stores number entered by the user
    for(i = 0; i < n; ++i)
    {
       printf("Enter Number %d: ", i+1);
       scanf("%f", &arr[i]);
    }

    // Loop to store largest number to arr[0]
    for(i = 1; i < n; ++i)
    {
       // Change < to > if you want to find the smallest element
       if(arr[0] < arr[i])
           arr[0] = arr[i];
    }
    printf("Largest element = %.2f \n", arr[0]);

    return 0;
}

Output:

Enter total number of elements(1 to 100): 5

Enter Number 1: 6
Enter Number 2: 8
Enter Number 3: 4
Enter Number 4: 2
Enter Number 5: 3
Largest element = 8.00
Press any key to continue . . .

Explanation:

Display Largest Element of an array

This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.