Find Second Largest Number in Array
Here you will learn and get the program code to find Second Largest Number in Array in C language. We will be implementing this program on 1d array in c programming.
Program to find Second Largest Number in Array in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | //find second largest number in array in c #include <stdio.h> int main() { int arr[10]; int size=5; // Input elements in array printf("\nInput 5 numbers :"); for(int i=0;i<5;i++) { scanf("\n%d",&arr[i]); } // find largest and second largest number int largest = arr[0]; int secondLargest = arr[0]; for (int i = 1; i < size; i++) { if (arr[i] > largest) { secondLargest = largest; largest = arr[i]; } else if (arr[i] > secondLargest && arr[i] != largest) { secondLargest = arr[i]; } } //print second largest number printf("\nSecond largest number is: %d\n", secondLargest); return 0; } |
Output
Input 5 numbers : 3 4 6 2 9
Second largest number is: 6
Check out our other C programming Examples