Program:
#include<stdio.h>
//#include<conio.h>
//void
main()
{
int i,l,a[100],sl,n;
printf("Enter the limit: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=a[0];
sl=a[1];
for(i=0;i<n;i++)
{
if(a[i]>l)
{
sl=l;
l=a[i];
}
}
for(i=0;i<n;i++)
{
if(a[i]>sl && a[i]!=l)
{
sl=a[i];
}
}
printf("largest=%d\n",l);
printf("second largest=%d\n",sl);
// getch();
}
Output:
nn@linuxmint ~ $ gcc c5.c
nn@linuxmint ~ $ ./a.out
Enter the limit: 5
Enter the elements:
5
4
3
2
1
largest=5
second largest=4
nn@linuxmint ~ $
#include<stdio.h>
//#include<conio.h>
//void
main()
{
int i,l,a[100],sl,n;
printf("Enter the limit: ");
scanf("%d",&n);
printf("Enter the elements:\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=a[0];
sl=a[1];
for(i=0;i<n;i++)
{
if(a[i]>l)
{
sl=l;
l=a[i];
}
}
for(i=0;i<n;i++)
{
if(a[i]>sl && a[i]!=l)
{
sl=a[i];
}
}
printf("largest=%d\n",l);
printf("second largest=%d\n",sl);
// getch();
}
Output:
nn@linuxmint ~ $ gcc c5.c
nn@linuxmint ~ $ ./a.out
Enter the limit: 5
Enter the elements:
5
4
3
2
1
largest=5
second largest=4
nn@linuxmint ~ $