Home / Programs / Display Smallest Element of an array
Programming Example

Display Smallest Element of an array

👁 1,142 Views
💻 Practical Program
📘 Step by Step Learning
Display Smallest Element of an array

Program Code

 /*
Display Smallest Element of an array
 Author: atnyla Developer
 */

  
#include"stdio.h"

int main()
{
    int i, n;
    float arr[100];

    printf("Enter total number of elements(1 to 100): ");
    scanf("%d", &n);
    printf("\n");

    // Stores number entered by the user
    for(i = 0; i < n; ++i)
    {
       printf("Enter Number %d: ", i+1);
       scanf("%f", &arr[i]);
    }

    // Loop to store largest number to arr[0]
    for(i = 1; i < n; ++i)
    {
       // Change < to > if you want to find the smallest element
       if(arr[0] > arr[i])
           arr[0] = arr[i];
    }
    printf("Largest element = %.2f \n", arr[0]);

    return 0;
}

Output

Enter total number of elements(1 to 100): 5

Enter Number 1: 1
Enter Number 2: 2
Enter Number 3: 3
Enter Number 4: 4
Enter Number 5: 5
Largest element = 1.00
Press any key to continue . . .

Explanation

Display Smallest Element of an array

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.