Program to find two largest number in array in a given Array is a simple c program that finds two maximum numbers in the given array.

Maximum number:
The number greater than any other number is the maximum number.

Example:
Numbers = 10, 3, 5, 6, 7
Two maximum numbers in array = 10 and 7

Algorithm to find top two max numbers in array

  1. Declare variable. N, a[], m1,m2,t
  2. Input Array values from user. a[N]
  3. Assign first and second value as m1 and m2
    m1= a[0] and a[1]
  4. If m1 < m2
    Swap m1 and m2
  5. Traverse array
    For i=2 to end
    If a[i] >= m1
    m2 = m1
    m1 = a[i]
    If a[i] > m2
    m2 = a[i]
  6. Print two maximum values. m1 and m2

Program to find two largest number in array

#include <stdio.h>
#define SIZE 4
void main()
{
int array[SIZE], i, max1, max2, temp;

printf("Enter %d integer numbers \n", SIZE);
for (i = 0; i < SIZE; i++)
{
scanf("%d", &array[i]);
}

/* assume first element of array is the first larges t*/

max1 = array[0];
/* assume second element of array is the second largest */

max2 = array[1];
if (max1 < max2)
{
temp = max1;
max1 = max2;
max2 = temp;
}

for (i = 2; i < 4; i++)
{
if (array[i] >= max1)
{
max2 = max1;
max1 = array[i];
}
else if (array[i] > max2)
{
max2 = array[i];
}
}

printf("%d is the first largest number \n", max1);

printf("%d is the second largest number \n", max2);

}

Output :

Program to find two largest number in array

Write A Comment