Program to find the largest and second largest nummber in a string of n numbers.

#include <stdio.h>
int main()
{
int num[50], n, large, largepos, slarge, slargepos, i;

printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Enter the numbers: ");
for(i=0;i<n;i++)
scanf("%d",&num[i]);

large=num[0];
largepos=0;
slarge=num[0];
slargepos=0;

for(i=0;i<n;i++)
{
if(num[i]>large)
{
slarge=large;
slargepos=largepos;
large=num[i];
largepos=i;
}
else if(num[i]>slarge)
{
slarge=num[i];
slargepos=i;
}
}

printf("The largest number is: %d Position: %d\nThe second largest number is: %d Position: %d\n",large, largepos,slarge, slargepos);

return 0;
}

Advertisements

One thought on “Program to find the largest and second largest nummber in a string of n numbers.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s