Second largest element in an array
Pre defined array: #include <stdio.h> int main() { int a[10] = {1, 11, 3, 4, 150, 69, 7, 8, 9, 10}; int i, l, s=0; l=a[0]; for(i=0; i<10; i++) { if(l<a[i]) l = a[i]; } for(i=0; i<10; i++) { if(a[i]>a[i+1] && a[i]!=l) s = a[i]; } printf("Largest - %d \nSecond - %d \n", l, s); return 0; } Taking array as an input: #include <stdio.h> int main() { int n, i, l, s=0; printf("Enter the number of element(s):\n"); scanf("%d", &n); int a[n]; for(i=0;i<n;i++) { printf("\nEnter the element %d:\t", i); scanf("%d", &a[i]); } l=a[0]; for(i=0; i<n; i++) { if(l < a[i]) { s = l; l = a[i]; } else if(s < a[i]) s = a[i]; } printf("\nLargest - %d \nSecond largest- %d \n", l, s); return 0;