Skip to main content

Write a program in C to find first and second largest Number from the inputted 1-D Array!


NOTE: Code was written in DEV C++

Basic requirement to learn this code
1.Array
2.Swap


#include<stdio.h>
main()
{
int i,n,fbig,sbig;

printf("\nenter the length of array");
scanf("%d",&n);
int a[n];
printf("input %d elements of array",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}

fbig=a[0];
sbig=a[1];
int temp;
if(sbig > fbig)
{
temp=fbig;
fbig=sbig;
sbig=temp;
}
for(i=2;i<n;i++)
{
if(a[i]>fbig && a[i]>sbig)
{
sbig=fbig;
fbig=a[i];
}
else if(fbig>a[i] && a[i]>sbig)
sbig=a[i];
}

printf("Here your 1st greatest no is %d and secondary greatest no. is %d",fbig,sbig);

}

Comments