# C Program to Find Smallest Element in an Array

Before going to the program first let us understand what is Smallest Element in an Array?

Smallest Element in Array:

Element which is having the Smallest value in a given array is called as the Smallest Element in an Array.

Example: Let us consider an array of values as given below:

a[]={3,4,9,6,1,8,5};

then,

The Smallest Element in an Array is “1”.

Now let us see the program code for Smallest Element in an Array and understand the code using the Explanation given below.

## Program code to find Smallest Element in an Array:

```#include<stdio.h>
#include<conio.h> void main()
{ int a,n,i,small; clrscr(); printf("n Enter the number of Elements: "); scanf("%d",&n); printf("n Enter %d Elements: ", n); for(i=0;i<n;i++) { scanf("%d",&a[i]); } small=a; for(i=1;i<n;i++) { if(small>a[i]) { small=a[i]; } } printf("n The Smallest Element in the given Array: %d",small); getch();
}
```

Related: C Program to Find the Largest Element in an Array

## Explanation:

• First the computer reads the number of elements in the array from the user and stores it in the “n” variable using the following lines:
```printf("n Enter the number of Elements: ");
scanf("%d",&n);
```

Note: %d is used to read the Integer value.

• Then it reads the elements one by one using the for loop and stored in the “a” array variable respectively using the following lines:
```printf("n Enter %d Elements: ", n);
for(i=0;i<n;i++)
{ scanf("%d",&a[i]);
}
```
• Then it assigns the value of a to “small” variable using the following line:
```small=a;
```
• Then using for loop and if condition the Smallest element is found and stored in “small” variable using the following lines:
```for(i=1;i<n;i++)
{ if(small>a[i]) { small=a[i]; }
}
```
• Finally The Smallest Element in the given Array is printed on the screen using the following line:
```printf("n The Smallest Element in the given Array: %d",small);
```

## Step by Step working of the above Program Code:

1. Let us assume that a user enters the Number of elements as 5.
2. Then the user enters 5 Elements as {6 4 7 2 5}.
3. So it stores the elements in the array a[]={6,4,7,2,5}.
4. Now it assigns the value of a to small (i.e. small=6).
5. Then it assigns the value of i=1 and the loop continues till the condition of the for loop is true.

5.1.   i<n    (1<5)    for loop condition is true

small>a[i]   (6>4)    if condition is true

small=a[i]    So  small=4

i++    (i=1+1)     So  i=2

5.2.  i<n    (2<5)    for loop condition is true

small>a[i]   (4>7)   if condition is false

i++    (i=2+1)     So  i=3

5.3.  i<n    (3<5)    for loop condition is true

small>a[i]   (4>2)    if condition is true

small=a[i]    So  small=2

i++    (i=3+1)     So  i=4

5.4.  i<n    (4<5)    for loop condition is true

small>a[i]   (2>5)    if condition is false

i++    (i=4+1)     So  i=5

5.5.  i<n    (5<5)    for loop condition is false

It comes out of the for loop.

1. Finally it prints The Smallest Element in the given Array: 2
2. Thus program execution is completed.

## Output: 