**C Program to Find the Largest Two Numbers in a given Array** is simple c program which finds two maximun number in given array.

In this tutorial we are going to learn a C Program to Find the Largest Two Numbers in a given Array.

This is simple program in c to implement C Program to Find the Largest Two Numbers in a given Array

**Maximum number :**

The number greater than any other number is maximum number.

**Minimum number :**

The number less than any other number is minimum number.

**How to find the largest two numbers in array**

First we take array input numbers from user. Then we take 2 variable as first max and second max. Now we traverse the array we compare the array element with first max element of it is max then we copy it to first max and then we compare first max with second if second max is greater than we copy replace second with first. In this way we find two maximum numbers in c

**Concept you need to know from c programming**

1. Basic input output

2. Condition and looping

3. Relational operators in c

**Algorithm to find top two max numbers in array**

1. Start

2. Declare variable

3. array input values from user

4. Find first and second max values

5. Print values

6. End

## Program to find two largest number in array

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#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 first 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 :**

**Explanation :**

**1. First we include the header files and define the size of array to 4**

**2. write a main function.**

i and temp => temporary variable

array => array to store 4 values

max1 and max2 => to store two max values

**3. Take input 4 values from user**

printf(“Enter %d integer numbers \n”, MAX);

for (i = 0; i < MAX; i++) => To take 4 inputs

scanf(“%d”, &array[i]) => take input in array

=> 20 , 30, 10, 50

**4. Now consider first element of array is first max value and second array element is second max value.**

=> max1 = 20 and max 2 = 30

**5. Compare max 1 and max 2 which one is maximum number if max2 is grater then swap max1 and max 2 values.**

if (max1 < max2) => 20 < 30 this condition is true so if block executed

temp = max1 => temp = 20

max1 = max2 => max1 = 30

max2 = temp => max2 = 20

**6. Next, we check top two maximum number in remaining numbers in array**

So we start for loop from 2 to end that is 4

for (i = 2; i < 4; i++) => to check two maximum from 2 to 4

**At iteration 1 :**

i = 2, 2 < 4 this condition becomes true and for loop executed

if (array[i] >= max1) => 10 >= 30 this condition becomes false if block skipped

else if (array[i] > max2) => 10 > 20 this conition becomes false and if skipped

**At iteration 2 :**

i = 3, 3 < 4 this condition becomes true and for loop executed

if (array[i] >= max1) => 50 >= 30 this condition becomes true and if block executed

max2 = max1 => max2 = 30

max1 = array[i] => max1 = 50

else if (array[i] > max2) is skipped

**At iteration 3 :**

i = 4, 4 < 4 this condition becomes false and executionexecution come out of for loop

**7. Print the two maximum numbers in array**

printf(“n%d is the first maximum number\n”, max1);

printf(“%d is the second maximum number\n”, max2);

=> 50 is the first maximum number

=> 30 is the second maximum number

**Recommended posts **

## Leave a Reply