Pages

Thursday, 2 July 2015

Smallest element

C Program to Find Smallest Element in Array in C Programming

Coding:

#include<stdio.h>
#include<stdlib.h>
int main()
{
float *a,min;
int n,i;
printf("Please enter total number of elements:");
scanf("%d",&n);
a=(float*)malloc(n*sizeof(float));
for(i=0;i<n;i++)
{
printf("Enter element %d:",i+1);
scanf("%f",&a[i]);
}
min=a[0];
for(i=1;i<n;i++)
{
if(min>a[i])
min=a[i];
}
printf("Minimum value is=%f",min);
return 0;
}

Output:
Please enter total number of elements:5
Enter element 1:9
Enter element 2:2
Enter element 3:5
Enter element 4:1
Enter element 5:7
Minimum value is=1.000000