C Program to Find Largest and Smallest Number in an Array

Here is the C program to find the largest and smallest element in a one-dimensional (1-D) array.

Output

How many elements:5
Enter the Array:1 8 12 4 6
The largest element is 12
The smallest element is 1

Explanation:

The program starts by declaring an integer array ‘a’ of size 50 elements, along with integer variables ‘i’, ‘n’, ‘large’, and ‘small’ to store the array index, array size, largest and smallest values respectively.

The program asks the user to enter the number of elements in the array and then reads in the integers from the user using a for a loop. It then uses another for loop to compare each element in the array to the previously largest and smallest values.

If an element is greater than the current largest, it replaces the current largest. Similarly, if an element is smaller than the current smallest, it replaces the current smallest.

Finally, the program gives the largest and smallest values found in the array using printf statements.

24 thoughts on “C Program to Find Largest and Smallest Number in an Array”

  1. 2 questions:

    1. How can I use this to compute difference between maximum and minimum

    2. How do I make it so when I input array, it is separated by commas

Leave a Comment

Your email address will not be published. Required fields are marked *