Program to find largest and smallest from an array

Program to find largest and smallest from an array

Program:


#include<stdio.h>
void main()
{
int i;
float a[5],m=0,n;
printf("Enter float array: \n");
for(i=0;i<5;i++)
{
scanf("%f",&a[i]);
if(a[i]>m)
{
m=a[i];
}
if(i==0)
{
n=a[i];
}
if(n>a[i])
{
n=a[i];
}
}
printf("\nSmallest of entered array is: %f",n);
printf("\nLargest of entered array is: %f",m);
}

Output:




Note:

We have assumed that user will enter only positive values and we have initialized largest element to 0 so that further elements are being compared with zero and if they have greater value then largest element will be stored in variable m (as in program).

No comments:

Post a Comment