C Program to find Second Largest number in an Array

Mr Coder August 17, 2013 6




C Program to find Second Largest number in an Array : Hey friends today we will learn how to write a C program 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 C program to find second largest number in an array 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.

6 Comments »

  1. vipin kumar September 21, 2013 at 7:07 am - Reply

    dekkers algorithm program in c

  2. Sobhan October 7, 2013 at 1:28 am - Reply

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

  3. rifleon February 24, 2014 at 7:28 am - Reply

    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

  4. krish September 9, 2014 at 1:33 pm - Reply

    i couldnot understand the algorithm

  5. Lavish September 11, 2014 at 7:55 am - Reply

    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);
    }

  6. Lavish September 11, 2014 at 7:57 am - Reply

    here is the correct program
    http://pastebin.com/gd5Ee1e1

Leave A Response »