C Program to find largest and smallest element in array


In this program, You will learn how to find largest and smallest element in array in c.


List is: 10 20 30 40 50 60

Largest and smallest element is: 60 10


Example: How to find largest and smallest element in array in c

Run it Now
#include<stdio.h>

int main() {

   int arr[5], i, lar, sm;

   printf("Enter 5 Numbers :");
   for (i = 0; i < 5; i++) {
       scanf("%d", &arr[i]);
   }

   sm = arr[0];
   lar = arr[0];

   for (i = 0; i < 5; i++) {
       if (sm > arr[i]) {
           sm = arr[i];
       }
       if (lar < arr[i]) {
           lar = arr[i];
       }
   }
   printf("\nThe Largest Element is :%d", lar);
   printf("\nThe Smallest Element is :%d", sm);

   return 0;
}

Output:

Enter 5 Numbers :12 13 5 6 7
The Largest Element is :13
The Smallest Element is :5