C Program to find Second Largest number in an Array

# C Program to find Second Largest number in an Array

25844
12
SHARE

C Program to find Second Largest number in an Array : Hey friends today we will learn how to write a to find the second largest number in an array. Logic is quite simple, first of all we will sort the elements in an array by using any sorting technique say bubble sort, selection sort or quicksort etc. Then we will use subscript indexing of array to display the second largest value say A[0] will contain the largest number, then A[1] will contain the second largest and so on.

Today i will show you how we can write a of 10 elements. This is sample program, we can generalize it to N element array just by adding few basic lines of codes to take an array size and elements as input and then scanf its values from user or we can randomly generate array elements.

# C Program to find Second Largest number in an Array:

```#include<stdio.h>
#include<conio.h>
int main()
{
int A[10];
int i , j , b;

for(i=0 ; i<10 ; i++)
{
printf(" Enter the Number %d : " , i+1 );
scanf("%d" , &A[i] );
}

for(i=0 ; i<10 ; i++)
{
for(j=i ; j<10 ; j++)
{
if(A[i] < A[j])
{
b = A[j] ;
A[j] = A[i] ;
A[i] = b ;
}
}
}

printf(" Second Largest Number is : %d\n" , A[1] );
getch();
}```

Sample Usage of the Program:

```Enter the Number 1 :-2
Enter the Number 2 :-5
Enter the Number 3 :0
Enter the Number 4 :1
Enter the Number 5 :6
Enter the Number 6 :-19
Enter the Number 7 :11
Enter the Number 8 :18
Enter the Number 9 :14
Enter the Number 10 :4
Second Largest Number is : 14```

We hope you all have enjoyed the C program to find the Second largest number in an array. If you have any queries ask us in form of comments.

SHARE
Next articleC Code to Convert NFA INTO DFA
Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games. Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com

1. @ADMIN……plz post some challenging C program which basically IT Company ask to do in there Technical round in campusing…..waiting ur post.

2. Hmm, I’m just thinking what will happen if I put two numbers as largest number, the program will show the largest number, not second largest number I think. :D

3. I think this program will work in better way
#include
int main()
{
int i,max=0,temp=0;
int a[] = {23,43,53,12,66,23,55,99,23,67,3};
for(i=0;imax)
{
temp = max;
max = a[i];
}
else
{
if(temp < a[i])
temp = a[i];
}
}
printf("Second largest element is %d",temp);
}

• Your Program will fail if there are duplicates. eg. int a[] = {23,43,99,12,66,23,99,23,67,3};
just modified your pgm to get this

#include
int main()
{
int i,max=0,sec=0;
int a[] = {1,9,8,3,9,13,7,4,5,13};
int n = sizeof(a)/sizeof(a[0]);
for(i=0;imax)
{
sec= max;
max = a[i];
}
else if(a[i]< max)
{
if(sec< a[i])
sec= a[i];
}

}
printf("Second largest element is %d\n",sec);
}

4. I want to design a program that arranges the preparation of an ascending order in C language and wants to use the macro and be a translator for the machine language