C Program to Find Smallest Element From Array

Question: Write a program in C to read an array containing n numbers and find smallest element from it.

C Program to Find Smallest Element From Array


#include<stdio.h>

int main()
{
 float a[50], sm;
 int i, n;
 
 printf("Enter n: ");
 scanf("%d", &n);
 /* Reading array */
 printf("Enter numbers:\n");
 for(i=0;i< n;i++)
 {
  printf("a[%d] = ",i);
  scanf("%f", &a[i]);
 }
 /* Finding small */
 sm = a[0];
 for(i=0;i< n;i++)
 {
  if(a[i] < sm)
  {
   sm = a[i];
  }
 }
 printf("\nSmall = %f", sm);
 
 return 0;
}

Output

Enter n: 5↲
Enter numbers:
a[0] = 21↲
a[1] = 0↲
a[2] = -78↲
a[3] = 77↲
a[4] = 9↲

Small = -78.000000