C Program to find Largest and Smallest Element in an Array

Largest and Smallest element in an array

Here we will discuss how to find the largest and smallest element in C. from inputted One Dimensional Array Elements. In this array we traverse elements recursively and encounter the value of smallest element and largest element until the end of the array with the help of concept of C and For loop in the code.
The solution of this problem is given in C programming language.

C-program-to-find-largest-and-smallest-element-in-an-array

Implementation:-

  • Take the size of array from the user
  • Input the array elements from the user
  • Initialize small = large = arr[0]
  • Repeat from i = 1 to size of array
  • if(arr[i] > large)
  • large = arr[i]
  • if(arr[i] < small)
  • small = arr[i]
  • Print the element as smallest and largest
Competitive Coding Techniques

C Code:-

//C Program to Find Largest and Smallest Element in an Array
#include <stdio.h>
int main()
    {
    int a[50], size, i, large, small;

    printf("Enter the size of the array: ");
    scanf("%d", &size);

    printf("Enter the %d elements of the array:\n", size);
    for(i = 0; i < size; i++)
    scanf("%d", &a[i]);

    large = a[0];
    for(i = 1; i < size; i++)
    {
        if(large < a[i])   // if larger value is encountered
        {
            large = a[i]; // update the value of large
        }
    }
    printf("The largest element is: %d",large);

    small = a[0];  
    for(i = 1; i < size; i++) { if(small>a[i])   // if smaller value is encountered
        {
            small = a[i];   // update the value of small
        }
    }
    printf("\nThe smallest element is: %d", small);
    return 0;
    }
Output:
Enter the size of the array: 6
Enter the 6 elements of the array:
9
32
14
75
100
55
The largest element is: 100
The smallest element is: 9