C Program to find the largest and smallest element in the array


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


List is: 10 20 30 40 50 60

Largest and smallest element is: 60 10

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

#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("Largest element is:%d", lar);
   printf("\nSmallest element is:%d", sm);

   return 0;
}

Output:

Enter 5 numbers:3 2 5 6 4                                                                                                              
Largest element is:6                                                                                                                   
Smallest element is:2