✓ Link copied to clipboard
C 📅 July 10, 2026 ⏱ 5 min read

C Program to Find Largest Element in an Array

C Program to Find Largest Element in an Array

Finding the largest element in an array is a common task in programming. Imagine needing to figure out the highest score in a list of students' marks. That's exactly what this program does. Let's dive into how we can do this using C.

What You Need Before Writing the Find Largest Element Program in C

Here's what you'll need:

  • Understanding of arrays and how to access elements.
  • Familiarity with loops, particularly for loops.
  • Any C compiler such as GCC 9 or newer — or just run it in the EduSeekho online compiler.

Source Code of C Program to Find Largest Element in an Array

Here's the complete code to find the largest element in an array:

largest_element.cc▶ Run
#include <stdio.h>

int main() {
    int n, i;
    int array[100];
    int max;

    // Read the number of elements
    printf("Enter the number of elements: ");
    scanf("%d", &n);

    // Read the array elements
    printf("Enter %d elements:\n", n);
    for (i = 0; i < n; i++) {
        printf("Enter element %d: ", i + 1);
        scanf("%d", &array[i]);
    }

    // Assume the first element is the largest initially
    max = array[0];

    // Iterate through the array to find the largest element
    for (i = 1; i < n; i++) {
        if (array[i] > max) {
            max = array[i];
        }
    }

    // Print the largest element
    printf("The largest element is: %d\n", max);

    return 0;
}

Try this code yourself — free

Run it in the EduSeekho online compiler. No setup needed.

▶ Open Compiler

Output of the Find Largest Element in an Array Program

When you run the program with the input 5, 3, 9, 1, 14, and 7, you'll get:

OUTPUTtext▶ Run
Enter the number of elements: 5
Enter element 1: 3
Enter element 2: 9
Enter element 3: 1
Enter element 4: 14
Enter element 5: 7
The largest element is: 14
Output of C Program to Find Largest Element in an Array
Output of C Program to Find Largest Element in an Array

Algorithm to Find the Largest Element in an Array in C

To find the largest element, the program compares each element with a variable holding the current maximum. Here's the step-by-step logic used:

  1. Read the integer n from the user.
  2. Read the array elements.
  3. Set max to the first element of the array.
  4. Loop through the array comparing each element with max.
  5. If an element is greater than max, update max.
  6. After the loop, max holds the largest element.
flowchart TD
  A([Start]) --> B[/Read n/]
  B --> C["Read array elements"]
  C --> D[max = array[0]]
  D --> E[i = 1]
  E --> F{"i < n?"}
  F -->|Yes| G{"array[i] > max?"}
  G -->|Yes| H[max = array[i]]
  G -->|No| I[i++]
  H --> I
  I --> F
  F -->|No| J([End])
Flowchart: the algorithm step by step

How the Find Largest Element Program Works

Let's go through the main parts of the code to understand how it works:

  • The program begins by including the standard input-output library #include <stdio.h>.
  • Read the number of elements into n using scanf.
  • The user is prompted to enter each element, which is stored in the array.
  • Initially, the first element array[0] is considered the largest and assigned to max.
  • A loop runs from the second element to the last, checking if each is larger than max. If so, it updates max.
  • The largest element is printed using printf.
  • The program returns 0 to signal successful completion.
🧠

Quick Quiz — test your understanding

Tap an option to check your answer

What is the initial value of the variable 'max' in this program?

🎉Correct! The initial value of 'max' is set to the first element of the array.
🤔Not quite. Consider how we compare each element to determine the largest.

How Fast Is the Find Largest Element Program in C?

Understanding the time and space complexity of this program helps in evaluating its efficiency.

Time Complexity: O(n). The program checks each of the n elements in the array once through the loop for (i = 1; i < n; i++). Since it processes every element one time linearly, it's O(n). For instance, 10 elements require 10 comparisons.

Space Complexity: O(1). The program uses a fixed number of variables such as n, i, array, and max, irrespective of the array size. The memory usage doesn't grow with input size because it's primarily determined by the input count and a few integers.

⚠️

Common mistake: Forgets to initialize max to the first element, which can lead to wrong results.

Key takeaway: Setting the maximum value with the first element is key for correctness.

FAQs About Finding the Largest Element in an Array in C

Here are some common questions students have about this program:

The program will return that same element as the largest since all are equal.
Yep, it treats negative numbers like any others and will find the largest.
This program assumes a non-empty array. You'd need extra checks for an empty array.
Change the comparison from array[i] > max to array[i] < min, and set min up correctly.
To make sure max always has a valid value to compare against the rest of the array.

If you found this program helpful, check out these related programs:

Conclusion

When you’re working with arrays, knowing how to find the largest element is fundamental. It's super useful in many scenarios like analyzing data sets or game scores. Next, you might wanna explore sorting arrays, which can also help identify extremes. Keep practicing with variations to really get the hang of it.

Put your skills to the test with our C quiz and try out the program using the online compiler.