English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C language basic tutorial

C language flow control

C Language Functions

C Language Arrays

C Language Pointers

C Language Strings

C language structure

C language file

C others

C language reference manual

C program to find the largest number in an array

Comprehensive Collection of C Programming Examples

In this example, you will learn to display the largest element entered by the user in the array.

To understand this example, you should understand the followingC programming languageTopic:

Find the largest element in the array

#include <stdio.h>
int main() {
    int i, n;
    float arr[100]);
    printf("Enter the number of elements (1 - 100): ");
    scanf("%d", &n);
    for (i = 0; i < n; ++i) {
        printf("Enter number %d: ", i + 1);
        scanf("%f", &arr[i]);
    }
    //Store the largest number in arr[0]
    for (i = 1; i < n; ++i) {
        if (arr[0] < arr[i])
            arr[0] = arr[i];
    }
    printf("The largest number is = %.2f", arr[0]);
    return 0;
}

Output result

Enter the number of elements (1 - 100): 5
Enter a number 1: 34.5
Enter a number 2: 2.4
Enter a number 3: -35.5
Enter a number 4: 38.7
Enter a number 5: 24.5
The largest number is = 38.70

The program gets n elements from the user and stores them in arr[].

To find the largest element,

  • Check the first two elements of the array and place the larger value in arr[0].

  • Check the first and third elements and place the larger of the two in arr[0].

  • This process continues until the first and last elements have been checked

  • The largest number will be stored at the arr[0] position

We use the for loop to complete this task.

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

Comprehensive Collection of C Programming Examples