# C Program to Calculate the Average of 3 Numbers using Function

In this article, we will write a C program to calculate the average of 3 numbers using function. The program takes three numbers from the user as input, calculates their average and prints the result on the output window.

Sample Example:

``````Enter three numbers: 10 20 30
Average of 10, 20 and 30 is: 20.000000

Enter three numbers: 5 2 7
Average of 5, 2 and 7 is: 4.666667``````

To calculate the average of three numbers using a user-defined function, we have to create a function that takes the three numbers as argument, calculates their average and returns the result.

As the average of three numbers can be an integer or a float value, therefore, we will keep the return type of the function a `float` which will take care of both integer and decimal point numbers. A `float` value is nothing but a decimal point value.

See implementation in the following program:

```// C program to find the average
// of three numbers using a function

#include <stdio.h>

// Function declaration
float average(int, int, int);

int main() {

int num1, num2, num3;
float avg;

printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

// Get average using average() function
avg = average(num1, num2, num3);

printf("Average of %d, %d and %d is: %f", num1, num2, num3, avg);
return 0;

}

// Function to calculate average of three numbers
float average(int x, int y, int z){

float avg;
avg = (x + y + z)/3.0;

return avg;
}
```

Output:

```Enter three numbers: 20 30 40
Average of 20, 30 and 40 is: 30.000000

Enter three numbers: 10 11 11
Average of 10, 11 and 11 is: 10.666667```

### Program Explanation:

• The program begins with the declaration of a function called `average()`, which takes three integer parameters (x, y, and z) and returns a floating-point value (avg).
• This function is declared before the `main()` function to inform the compiler about its existence.
• Variables `num1`, `num2`, and `num3` are used to store user-input numbers and a floating-point variable `avg` to store the calculated average.
• To calculate the average of the three input numbers, the `average()` function is called inside the main() function with the values of num1, num2, and num3 as arguments.
• The result is stored in the `avg` variable.
• Finally, the program uses `printf()` to display the calculated average along with the input values.

• 