# C program to find second largest number in an array

Write a C program to find the largest and second largest element in an array. How to find second largest element of an array in C programming language. Logic to find second largest element of an array in C program.

Example

Input

```Input array elements: -7 2 3 8 6 6 75 38 3 2
```

Output

```Second largest = 38
```

## Required knowledge

Basic C programming, Array

Must know - Program to find largest element in an array

## Logic to find second largest element

Below is the step by step descriptive logic to find second largest element in an array.

1. Input elements in an array, in some variable say array.
2. Declare two variables to store first and second maximum values. Assume that the zeroth array element is the first as well as second largest element. Say max1 = array[0] and max2 = array[0].
3. Run a loop from 0 to N-1, the loop structure should look like for(i=0; i<N; i++).
4. Inside the loop, check if current array element is greater than max1, then make the largest element as second largest and current array element as largest element. Say, max2 = max1 and max1 = array[i].
5. Else if the current array element is greater than max2 but is less than max1 then make current array element as second largest element i.e. max2 = array[i].

### Algorithm to find second largest element in array

```Algorithm to find second largest element in array
%%Input : arr {Array in which largest is to be found}
N {Size of the array}
Begin:
max1 ← arr[0];
max2 ← arr[0];
For i ← 1 to N do
If (arr[i] > max1) then
max2 ← max1;
max1 ← arr[i];
End if
Else if (arr[i] > max2 && arr[i] < max1) then
max2 ← arr[i];
End if
End for
End```

## Program to find second largest element in array

``````/**
* C program to find second largest number in an array
*/

#include <stdio.h>
#include <limits.h> //For INT_MIN

#define MAX_SIZE 1000

int main()
{
int arr[MAX_SIZE], N, i;
int max1, max2;

/* Input size of the array */
printf("Enter size of the array (1-1000): ");
scanf("%d", &N);

/* Input array elements */
printf("Enter elements in the array: ");
for(i=0; i<N; i++)
{
scanf("%d", &arr[i]);
}

max1 = max2 = INT_MIN;

/*
* Check for first largest and second largest till N
*/
for(i=0; i<N; i++)
{
if(arr[i] > max1)
{
/*
* If current element of the array is first largest
* then make current max as second max
* and then max as current array element
*/
max2 = max1;
max1 = arr[i];
}
else if(arr[i] > max2 && arr[i] < max1)
{
/*
* If current array element is less than first largest
* but is greater than second largest then make it
* second largest
*/
max2 = arr[i];
}
}

printf("First largest = %d\n", max1);
printf("Second largest = %d", max2);

return 0;
}``````

Output

```Enter size of the array (1-1000): 10
Enter elements in the array: -7 2 3 8 6 6 75 38 3 2
First largest = 75
Second largest = 38```

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside