Given an array from user the task is to find both smallest and largest elements simultaneously from the array.
Example:
Consider an array of size 5.
| 2 | 7 | 1 | 4 | 8 |
Here the largest element is 8 and smallest element is 1.
C
#include <stdio.h>
int main()
{
int n=0,max=0,min=0;
printf("Enter the array size: ");
scanf("%d",&n);
int a[n];
printf("Enter the array elements:-\n");
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
max=a[0];
min=a[0];
for(int i=0;i<n;i++)
{
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
printf("Largest Element: %d\n",max);
printf("Smallest Element: %d",min);
return 0;
}C