You are here

C program to find maximum and minimum number in an array

C program to find the maximum and minimum number in an array. A user will input the array and we'll find the largest and the smallest number in the array.

C program

#include <stdio.h>

int main()
{
  int a[100],size, c, l, L;

  printf("Enter the number of elements in array\n");
  scanf("%d", &size);

  printf("Enter %d integers\n", size);

  for (c = 0; c < size; c++)
    scanf("%d", &a[c]);

  l = L = 0;

  for (c = 1; c < size; c++) {
    if (a[c] > a[L])
      L = c;
    else if (a[c] < a[l])
      l = c;
  }

  printf("Minimum element is present at location %d and its value is %d.\n", l + 1, a[l]);
  printf("Maximum element is present at location %d and its value is %d.\n", L + 1, a[L]);

  return 0;
}