Programming Example
Display Smallest Element of an array
Display Smallest Element of an array
/*
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;
}
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 . . .
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.