February 18, 2022, Learn eTutorial

1085

In this c program, we need to calculate the largest of the two numbers from the input of 4 digits. After finding the 2 largest numbers, we need to calculate the sum of the two largest numbers. Finally, divide the sum by two to get the average of two numbers.

The logic used in this C program is to put the elements in an Array, and then we assign the '**L1**' as an Array first element and '**L2**' as Array second element. Compare the '**L1**' and '**L2**' using the '`if`

** **'condition, if '**L2**' is Larger than '**L1**', then `Swap`

the values of '**L1**' and '**L2**' using a **temp** variable.

Compare '**L1**' with the Third element; if that is greater than '**L1**', change '**L1**' to '**L2**' and Third element as '**L1**'. If the Third is greater than '**L2**' make the Third element as '**L2**' after that loop, we get '**L1**' and '**L2**' as the Largest and Second Largest element of the Array. We sort the Array we have to take the Sum of '**L1**' and '**L2**' and divide the sum with **2** to find the Average.

**STEP 1:** Import the Header files into the C program to use the built-in functions.

**STEP 2:** Initialize and Define the Array and other variables in the program.

**STEP 3:** Accept the numbers from the user and add that numbers into an Array using `scanf`

and `for loop`

.

**STEP 4:** Display the Array using `for loop`

and `printf`

.

**STEP 5:** Assume and Assign the first two elements of the Array to **L1** and **L2**.

**STEP 6:** Using an '`if`

' condition check **L1 **is greater than **L2**.

**STEP 7:** If not, Swap the value of **L1** and **L2** using a **Temp** variable.

**STEP 8:** Open a `for loop`

from 2 to max value increment by checking the Largest element.

**STEP 9:** Check if the Array's third element is Larger than **L1**, then Swap **L2** as **L1 **and** L1 **as Array third element.

**STEP 10:** Else if the Array third element is Greater than **L2**, then swap **L2** as Array third element.

**STEP 11:** Finally, after Iteration of `for loop`

, we have the Largest element in **L1** and **L2**, then add **L1** and **L2** and Divide it with 2 to get Average.

**STEP 12: **Print the Average using the `printf`

in the C programming language.

` ````
#include <stdio>
#include <conio>
#define MAX 4
void main() {
int a[MAX], i, l1, l2, temp; /* declares array and variables */
clrscr();
printf("Enter %d integer numbers\n", MAX);
for (i = 0; i < MAX i = 0; xss = removed xss = removed xss = removed xss = removed xss = removed i = 2; i >= l1) /* comparing and interchanging the values in the array to get l1 and l2 as largest */ {
l2 = l1;
l1 = a[i];
} else if (a[i] > l2) {
l2 = a[i];
}
}
printf("\n%d is the first largest\n", l1); /* displays the value of l1 and l2 in the array */
printf("%d is the second largest\n", l2);
printf("\nAverage of ?nd %d = %d\n", l1, l2, (l1 + l2) / 2); /* calculate and display the average of the two numbers */
}
```

RUN 1 Enter 4 integer numbers 45 33 21 10 Input integers are 45 33 21 10 45 is the first largest 33 is the second largest Average of 45 and 33 = 39 RUN 2 Enter 4 integer numbers 12 90 54 67 Input integers are 12 90 54 67 90 is the first largest 67 is the second largest Average of 90 and 67 = 78 RUN 3 Enter 4 integer numbers 100 200 300 400 Input integers are 00 200 300 400 400 is the first largest 300 is the second largest Average of 400 and 300 = 350