C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Arrays in C

Maximum in array

C Program: Maximum in array

Method 1: Using for loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i, max;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    for(i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

 

    // Assume first element as maximum

    max = arr[0];

 

    // Find maximum element

    for(i = 1; i < n; i++) {

        if(arr[i] > max) {

            max = arr[i];

        }

    }

 

    // Display result

    printf("\nMaximum element in the array = %d\n", max);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
12 45 7 89 34

OUTPUT :
Maximum element in the array = 89

Explanation

  1. The user enters how many elements (n) to store in the array.
  2. The program reads all elements using a for loop.
  3. Initially, the first element (arr[0]) is assumed to be the maximum.
  4. Then, the loop compares each element with max — if a larger value is found, max is updated.
  5. Finally, the largest number is displayed.
  6. The max-finding algorithm scans the array once, comparing elements sequentially.
  7. Time Complexity: O(n) — because each element is checked exactly once.
  8. Works efficiently for both positive and negative integers.

 

C Program: Maximum in array

Method 2: Using while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, max;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements

    printf("Enter %d elements:\n", n);

    while(i < n) {

        scanf("%d", &arr[i]);

        i++;

    }

 

    // Assume first element as maximum

    max = arr[0];

 

    // Find maximum element

    i = 1;

    while(i < n) {

        if(arr[i] > max) {

            max = arr[i];

        }

        i++;

    }

 

    // Display result

    printf("\nMaximum element in the array = %d\n", max);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 6
Enter 6 elements:
15 7 99 45 23 78

OUTPUT :
Maximum element in the array = 99

Explanation

  1. The program first asks the user for the number of array elements (n).
  2. Using a while loop, it inputs all elements of the array.
  3. The first element is assumed to be the maximum.
  4. Another while loop is used to compare each subsequent element with max.
    • If a larger number is found, max is updated.
  5. After all elements are checked, the largest value is printed.
  6. The while loop gives fine control over the iteration process.
  7. The comparison step ensures only one pass through the array (O(n) time).
  8. Works for both positive and negative integers.

 

C Program: Maximum in array

Method 3: Using do..while loop

C

#include <stdio.h>

 

int main() {

    int arr[100], n, i = 0, max;

 

    // Input number of elements

    printf("Enter number of elements: ");

    scanf("%d", &n);

 

    // Input array elements using do...while loop

    printf("Enter %d elements:\n", n);

    do {

        scanf("%d", &arr[i]);

        i++;

    } while(i < n);

 

    // Assume first element as maximum

    max = arr[0];

 

    // Find maximum element using do...while loop

    i = 1;

    do {

        if(arr[i] > max) {

            max = arr[i];

        }

        i++;

    } while(i < n);

 

    // Display result

    printf("\nMaximum element in the array = %d\n", max);

 

    return 0;

}

Output

 
INPUT :
Enter number of elements: 5
Enter 5 elements:
25 67 12 89 45

OUTPUT :
Maximum element in the array = 89

Explanation

  1. The user is prompted to enter the number of array elements (n).
  2. Using a do..while loop, the program takes n elements as input.
    • This ensures that the loop executes at least once, even if n = 1.
  3. The first element is initially assumed to be the maximum.
  4. Another do..while loop compares every element with max.
    • If any element is greater, max gets updated.
  5. Finally, the maximum value is displayed.
  6. The ..while loop guarantees at least one execution before checking the condition.
  7. It is particularly useful when input operations must occur regardless of array size.
  8. Time complexity remains O(n) since each element is checked exactly once.