# Find Largest and Smallest element in Array by index in C

In this article, we are going to find the largest and smallest element in an array. We will ask the user to create an array of random input numbers and once this array is created, we will search the smallest and largest number in it.

### How does it works

We will take two variables for maxNum and minNum and assign them both to the first element of the array. We will then iterate on the array to check if the next element is smaller or larger than the first element.

According to the result, we will update the values of maxNum and minNum.We will keep doing this until we reach the end of the array. In the end, we will have the smallest and largest values in the minNum and maxNum variables. let us now see this logic in action in the below C program.

### C Program to Find Largest and Smallest element in Array by index

```#include<stdio.h>
int main()
{
int arr[10], len;
int maxNum, minNum, maxPos, minPos;

printf("Please enter the Size of array with Max size 10: ");
scanf("%d",&len);

maxPos=minPos=0;

printf("Please enter the elements in this Array: ");
for(int i = 0; i < len; i++)
scanf("%d", &arr[i]);

maxNum = arr[0];
minNum = arr[0];

for(int i = 1; i < len; i++)
{

if(maxNum < arr[i])
{
minNum = arr[i];
minPos = i;
}

if(minNum > arr[i])
{
minNum = arr[i];
minPos = i;
}
}

printf("The Largest element is %d at %d position.\n", maxNum,maxPos);

printf("The Smallest element is %d at %d position.", minNum,minPos);

return 0;
}
```

Output

```Please enter the Size of array with Max size 10: 5
Please enter the elements in this Array: 3 6 9 12 15
The Largest element is 3 at 0 position.
The Smallest element is 15 at 4 position.
```