Follow Us

LightBlog

Breaking

Tuesday, February 28, 2017

Find the second largest element in an array ?


             C program to find the second largest element in an array.
  
#include<stdio.h>
#include<conio.h>
int main()
{
  int a[50],size,i,j=0,b,s;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter %d Elements in to the array: ", size);
  for(i=0;i<size;i++)

   scanf("%d",&a[i]);

  b=a[0];
  for(i=1;i<size;i++)
{
      if(b<a[i])
{
           b=a[i];
           j = i;
      }
  }

  s=a[size-j-1];
  for(i=1;i<size;i++)
{
      if(s <a[i] && j != i)
          s =a[i];
  }
  
  printf("Second biggest: %d", s);
  return 0;
}
output:
Enter the size of the array: 5
Enter 5 Elements in to the array: 5 3 2 1 0
Second biggest: 3

No comments:

Post a Comment