Find the Maximum Element in a 1D Array in C

Written: August 14, 2026 · Updated: August 15, 2026

Finding a maximum is the pattern behind many later algorithms: keep a champion, challenge it with each next value.

This walkthrough keeps the logic small and readable so you can type it, run it, and then change one input at a time to see what happens.

What you will build

Initialize the max with the first element, then walk the rest of the array updating whenever you see a larger value.

  • Read n and the array
  • Set max = a[0]
  • Loop from index 1 to n-1 updating max
  • Print max

Working C example

include <stdio.h>

int main(void) {
    int n, i, max;
    int a[100];
    printf("Enter count (1-100): ");
    if (scanf("%d", &n) != 1 || n < 1 || n > 100) return 1;
    for (i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) return 1;
    }
    max = a[0];
    for (i = 1; i < n; i++) {
        if (a[i] > max) max = a[i];
    }
    printf("Maximum = %d\n", max);
    return 0;
}

Advertisement

How the logic works

Read the program top to bottom: includes and main first, then the statements that change variables, then the print that proves the result.

If your output looks wrong, print the variables before and after the critical lines. That single habit catches most beginner bugs faster than rewriting the whole file.

Starting max at 0 fails for all-negative arrays. Starting from a[0] avoids that trap.

Advertisement

Keep learning

If this walkthrough on Find the Maximum Element in a 1D Array in C helped, open the code again and change one input or assumption. Small experiments beat rereading the same example.

Want more step-by-step tutorials like this? Browse blog.xqa.io — and tell us which topic you want next.

Advertisement

Previous Article

C Program to Check Positive, Negative, or Zero

Next Article

C Program for Sum of First and Last Digit

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨