Sunday, 19 October 2014

C Program for finding Second Largest Number in the given array of elements

Program

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],i,j,temp,n;
        clrscr( );//clear the screen contents
printf("\nEnter size of the Array:");
scanf("%d",&n);
printf("\nEnter elements to Array:");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)// Sorting logic begins
{
for(j=0;j<n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}//ends
printf("Second largest: %d",a[n-2]);
getch();
}


Input/Output:

Enter size of the Array: 5

Enter elements to Array:5
4
8
2
1
Second largest: 5



0 comments:

Post a Comment