C Program to Calculate Average of an Array Last Updated : 21 Nov, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to calculate the average of all elements of an array using a C program.The simplest method to calculate the average of all elements of an array is by using a loop. Let's take a look at an example: C #include <stdio.h> float getAvg(int arr[], int n) { int sum = 0; // Find the sum of all elements for (int i = 0; i < n; i++) { sum += arr[i]; } // Return the average return (float)sum / n; } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); // Calculate the average of array arr float res = getAvg(arr, n); printf("%.2f\n", res); return 0; } Output3.00 Explanation: This method iterates through the entire array to find the sum of all elements and then divides the sum by the total number of elements to calculate the average.This approach can also be implemented using recursion as shown below.Using RecursionThis method computes the average of the first n-1 elements using a recursive call for each element of the array. Then it combines it with the average of last element to find the overall average. C #include <stdio.h> float getAvg(int arr[], int n) { // If only one element left, return it if (n == 1) { return arr[0]; } // Add the current element to the average of // the remaining array return (arr[n - 1] + (n - 1) * getAvg(arr, n - 1)) / n; } int main() { int arr[] = {1, 2, 3, 4, 5}; int n = sizeof(arr) / sizeof(arr[0]); // Calculate the average float res = getAvg(arr, n); printf("%.2f", res); return 0; } Output3.00 Comment More infoAdvertise with us Next Article C Program to Calculate Average of an Array mukulsomukesh Follow Improve Article Tags : C Programs C Language C Array Programs Similar Reads C Program to Traverse an Array Write a C program to traverse the given array that contains N number of elements.ExamplesInput: arr[] = {2, -1, 5, 6, 0, -3} Output: 2 -1 5 6 0 -3Input: arr[] = {4, 0, -2, -9, -7, 1} Output: 4 0 -2 -9 -7 1Different Ways to Traverse an Array in CArrays are versatile data structures and C language pro 3 min read Array C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum as xC/C++ Program for Majority ElementC/C++ Program for Find the Number Occurring Odd N 6 min read How to Initialize a Dynamic Array in C? In C, dynamic memory allocation is done to allocate memory during runtime. This is particularly useful when the size of an array is not known at compile time and needs to be specified during runtime. In this article, we will learn how to initialize a dynamic array in C.Initializing a Dynamic Arrays 2 min read How to Find the Size of an Array in C? The size of an array is generally considered to be the number of elements in the array (not the size of memory occupied in bytes). In this article, we will learn how to find the size of an array in C.The simplest method to find the size of an array in C is by using sizeof operator. First determine t 2 min read How to Find Median of Numbers in an Array in C? For an odd number of elements in an array, the median is the middle element, and for an even number, itâs the average of the two middle elements. In this article, we will learn how to find median of numbers in an array in C.The median of an array can be determined is by first sorting the array and t 3 min read How to Initialize Array to 0 in C? Initializing an array to zero is a common practice in programming to ensure that all elements start with a known value. In C, there are several ways to initialize an array to zero. In this article, we will explore different methods to do so. Initialize Array to 0 in CThere are mainly two ways to ini 2 min read How to Find Size of Dynamic Array in C? In C, dynamic memory allocation allows us to manage memory resources during the execution of a program. Itâs particularly useful when dealing with arrays where the size isnât known at compile time. In this article, we will learn how to find the size of a dynamically allocated array in C. Find Size o 2 min read C Program to Calculate Sum of Array Elements In this article, we will learn how to find the sum of elements of an array using a C program.The simplest method to calculate the sum of elements in an array is by iterating through the entire array using a loop while adding each element to the accumulated sum.C#include <stdio.h> int getSum(in 2 min read C++ Program For Average of an Array (Iterative and Recursive) Given an array, the task is to find the average of that array. Average is the sum of the array elements divided by the number of elements. Examples: Input: arr[] = {1, 2, 3, 4, 5}Output: 3Sum of the elements is 1+2+3+4+5 = 15 and total number of elements is 5.So average is 15/5 = 3 Input: arr[] = {5 3 min read How to Calculate the Average using Arrays in Golang? Given an array of n elements, your task is to find out the average of the array.Approach:Â Accept the size of the array.Accept the elements of the array.Store the sum of the elements using for loop.Calculate Average = (sum/size of array)Print the average. Example:Â Input: n = 4 array = 1, 2, 3, 4 Ou 2 min read Like