C Program to Find Maximum Element in Array

1. Introduction

In many scenarios, one may need to find the maximum element in an array, be it for analytics, data processing, or optimizing solutions. This post will guide you on writing a C program to find the maximum element in an array and will break down the steps to understand its working.

2. Program Overview

Our C program will do the following:

1. Accept the number of elements and the array of elements from the user.

2. Traverse the array and find the maximum element.

3. Display the maximum element to the user.

3. Code Program

#include <stdio.h>  // Including standard input/output library

int main() {  // Main function start
    int n, i;
    int arr[100];  // Array declaration
    int max;       // To store maximum value
    // Get number of elements from user
    printf("Enter the number of elements in array: ");
    scanf("%d", &n);
    // Input elements
    printf("Enter %d integers:\n", n);
    for(i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    max = arr[0];  // Assume first value to be the maximum
    // Traverse array elements 
    for(i = 1; i < n; i++) {
        if(arr[i] > max) {
            max = arr[i];  // Update the max value
    // Print the maximum value
    printf("Maximum element in the array is: %d", max);
    return 0;  // End of the program


Enter the number of elements in array: 5
Enter 5 integers:
Maximum element in the array is: 80

4. Step By Step Explanation

1. Header File: We start with the stdio.h header, which gives us access to the standard input and output functions in C.

2. Variable Declaration:

  • n is for storing the number of elements.
  • arr[100] holds the array values.
  • max stores the maximum value.

3. Taking Input:

  • We ask the user for the number of elements and then take input for each of these elements.

4. Finding Maximum:

  • Initially, we assume the first element is the maximum.
  • Then, we traverse the array. If we encounter an element larger than our current maximum (max), we update max with this new value.

5. Displaying Result: The maximum value is printed to the console.

By the end of this program, you will have learned an efficient way to find the maximum element in an array. This method uses a single loop, ensuring performance and speed.