# Average of Two Numbers in C

## What is Average?

In mathematics, the average is known as the mean. It is the measure of central tendency. It represents the typical or representative value of a set of numbers. To Find the average of numbers, we add up all the numbers given and divide the sum by the total number of values in the set.

average = sum of all values/number of values

For example, let us consider that the given numbers are {2, 4, 6, 8, 10}. The average of these numbers can be calculated by adding all the numbers together (2 + 4 + 6 + 8 + 10 = 30) and dividing by the total number of values in the set, 5. In This way, the average of the number we get is 6.

## How to Find Average of Two Numbers

Two methods can calculate the average of two numbers:

- The first method is the
**standard method**, where we prompt the user to input two integer-type numbers, store them in the variables (say number 1 and number 2), calculate their average, and print the output on the console. - The second method
**uses a function**where we prompt the user to input two integer-type numbers, store them in the variables (say number 1 and number 2), call the function, calculate its average and print the output on the console.

## C Program to Find the Average of Two Given Numbers

We are going to find the average of two numbers whose values have been defined earlier:

**Output :**

In this example, we use the mathematical formula to find the average of numbers. Since we need to find the average of two numbers, we have divided the sum of these numbers by 2.

Inside the main() function, we have declared three variables, x, y, and the result of integer type, and initialized the variables num1 and num2 with values 12 and 16, respectively.

After that, we calculated the average by adding num1 and num2, dividing the sum by 2, and assigning the calculated average to the variable result.

## C Program to Find the Average of Two Numbers by Taking Input From the User

Now we will find the average of two numbers where we will take the user's input.

- Initially, the program will prompt the user to enter numbers one and two and store the value in these variables. Float average declares that the average of two numbers will be of
**float datatype**. - After the values are stored, the average is calculated using average= (float)(number1 + number2)/2; here, the result of the two numbers is converted into float datatype and then stored on average.
- After the calculation, the output gets printed on the console.

Output :

This example works the same as the above except that in the below example, we have taken the input from the users, stored the inputs in the variables num1 and num2 by scanf function, and then calculated the average.

## How to Find the Average of Two Numbers Using Function in C

- Initially, the program will prompt the user to enter the two numbers and store the values in variables (number 1 and number 2).
- Then the numbers are passed in avg = average(number1, number2), where the user-defined average function is called, and inputs are pushed into the function.
- In float average(int x, int y),
**int x, int y**represents**number 1**and**number 2**. The inputs are passed as arguments in the function, and then the average of those numbers is calculated using return (float)(x + y)/2. - After calculating the output, it is returned and printed on the console.

**Output :**

Note:In the above program, %.2f displays floating point numbers up to two decimal places.

## C Program to Calculate Average Using Arrays

### Algorithm to Find Average of Numbers

**START**

Step 1: Collect integer values in an array A of size N

Step 2: Add all values of A

Step 3: Divide the output of Step 2 with N

Step 4: Display the output of Step 3 as average

**STOP**

**Output :**

## Find Average of Given Numbers Using While Loop

**Output:**

Here, the user is first asked to enter the number of elements. This number is assigned to n.

If the user enters an integer greater than one or greater than 100, the user is asked to enter the number again. This is done using a while loop.

Then, we iterated a for loop from i = 0 to i. In each iteration of the loop, the user is asked to enter numbers to calculate the average. These numbers are stored in the num[] array.

Then the Sum of Each Entered element is calculated.

Once the for loop is completed, the average is calculated and printed on the screen.