2/22/2009

C Program To Find Second Largest Number In A Given Set Of Elements


/* C Program To Find Second Largest Number In A Given Set Of Elements */
#include<stdio.h>
#include<conio.h>
#include<math.h>


int main()
{
   int a[50],size,i,j=0,large,slarge;
  printf("Enter the size of the array: ");
  scanf("%d",&size);
  printf("Enter the elements one by one:\n");
  for(i=0;i<size;i++)
      scanf("%d",&a[i]);

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

  slarge=a[size-j-1];
  for(i=1;i<size;i++){
      if(slarge <a[i] && j != i)
          slarge =a[i];


    }
        printf("Second largest: %d", slarge);
        return 0;
}
Out Put:
Enter the size of the array: 8
Enter the elements one by one:
5
9
4
8
2
1
3
6
Second largest: 8

No comments:

Post a Comment