C Exercises: Find the second smallest element in an array
C Array: Exercise-17 with Solution
Write a program in C to find the second smallest element in an array.
Pictorial Presentation:

Sample Solution:
C Code:
#include <stdio.h>
void main()
{
int arr1[50],n,i,j=0,sml,sml2nd;
printf("\n\nFind the second smallest element in an array :\n");
printf("--------------------------------------------------\n");
printf("Input the size of array : ");
scanf("%d", &n);
/* Stored values into the array*/
printf("Input %d elements in the array (value must be <9999) :\n",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/* find location of the smallest element in the array */
sml=arr1[0];
for(i=0;i<n;i++)
{
if(sml>arr1[i])
{
sml=arr1[i];
j = i;
}
}
/* ignore the smallest element and find the 2nd smallest element in the array */
sml2nd=99999;
for(i=0;i<n;i++)
{
if(i==j)
{
i++; /* ignoring the smallest element */
i--;
}
else
{
if(sml2nd>arr1[i])
{
sml2nd=arr1[i];
}
}
}
printf("The Second smallest element in the array is : %d \n\n", sml2nd);
}
Sample Output:
Find the second smallest element in an array : -------------------------------------------------- Input the size of array : 5 Input 5 elements in the array (value must be <9999) : element - 0 : 0 element - 1 : 9 element - 2 : 4 element - 3 : 6 element - 4 : 5 The Second smallest element in the array is : 4
Flowchart:

C Programming Code Editor:
Improve this sample solution and post your code through Disqus.
Previous: Write a program in C to find the second largest element in an array.
Next: Write a program in C for a 2D array of size 3x3 and print the matrix.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
C Programming - What is the argument for printf that formats a long?
Put an l (lowercased letter L) directly before the specifier.
unsigned long n; long m; printf("%lu %ld", n, m);
Ref : https://bit.ly/3dIwfkP
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises