C Code 12 _ To Find the largest , second largest ,smallet and second smallest elements in a array
Program:
#include<stdio.h>
int main()
{
int n,i,j,temp;
int a[50];
printf("Enter the number of elements : ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i;j<n;j++)
{
if(a[j]<a[i])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\nThe largest element is %d",a[n-1]);
printf("\nThe Second largest element is %d",a[n-2]);
printf("\nThe Smallest element is %d",a[0]);
printf("\nThe Second Smallest element is %d",a[1]);
return 0;
}
Output:
Thank You ...
0 comments: