Program for average of an array (Iterative and Recursive)

11 Oct, 2021
Given an array, the task is to find average of that array. Average is the sum of array elements divided by the number of elements.
Examples : 
Input : arr[] = {1, 2, 3, 4, 5}
Output : 3
Sum 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, 6, 7, 5, 3}
Output : 4.83333
Sum of the elements is 5+3+6+7+5+3 = 29
and total number of elements is 6.
So average is 29/6 = 4.83333.
Iterative: Iterative program is easy. We need to find sum and divide sum by total number of elements. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to calculate average of array elements#include <iostream>using namespace std; // Function that return average of an array.double average(int a[], int n){    // Find sum of array element    int sum = 0;    for (int i=0; i<n; i++)       sum += a[i];     return (double)sum/n;} // Driver codeint main(){    int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};    int n = sizeof(arr)/sizeof(arr[0]);     cout << average(arr, n) << endl;    return 0;}
// Java program to calculate average of array elementsclass GFG {         // Function that return average of an array.    static double average(int a[], int n)    {                 // Find sum of array element        int sum = 0;                 for (int i = 0; i < n; i++)            sum += a[i];             return (double)sum / n;    }         //driver code    public static void main (String[] args)    {                 int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};        int n = arr.length;             System.out.println(average(arr, n));    }} // This code is contributed by Anant Agarwal.
# Python3 code to calculate# average of array elements # Function that return# average of an array.def average( a , n ):     # Find sum of array element    sum = 0    for i in range(n):        sum += a[i]         return sum/n; # Driver codearr = [10, 2, 3, 4, 5, 6, 7, 8, 9]n = len(arr)print(average(arr, n)) # This code is contributed by "Shard_Bhardwaj".
// C# program to calculate average of// array elementsusing System;class GFG {         // Function that return average of    // an array.    static double average(int []a, int n)    {                 // Find sum of array element        int sum = 0;                 for (int i = 0; i < n; i++)            sum += a[i];             return (double)sum / n;    }         // Driver code    public static void Main ()    {                 int []arr = {10, 2, 3, 4, 5, 6,                                7, 8, 9};        int n = arr.Length;             Console.Write(average(arr, n));    }} // This code is contributed by nitin mittal.
<?php// PHP program to calculate average// of array elements // Function that return average of// an array.function average( $a, $n){         // Find sum of array element    $sum = 0;    for ( $i = 0; $i < $n; $i++)        $sum += $a[$i];     return $sum / $n;} // Driver code    $arr = array(10, 2, 3, 4, 5,                        6, 7, 8, 9);    $n = count($arr);     echo average($arr, $n); // This code is contributed by anuj_67.?>
<script>      // JavaScript program to calculate average of array elements       // Function that return average of an array.      function average(a, n)      {               // Find sum of array element        var sum = 0;        for (var i = 0; i < n; i++) sum += a[i];         return parseFloat(sum / n);      }       // Driver code       var arr = [10, 2, 3, 4, 5, 6, 7, 8, 9];      var n = arr.length;       document.write(average(arr, n));      document.write("<br>");             // This code is contributed by rdtank.    </script>
6
Time Complexity: O(n)
Recursive: The idea is to pass index of element as an additional parameter and recursively compute sum. After computing sum, divide the sum by n.
C++
Java
Python3
C#
PHP
Javascript
// C++ program to calculate average of array elements#include <iostream>using namespace std; // Recursively computes average of a[]double avgRec(int a[], int i, int n){    // Last element    if (i == n-1)        return a[i];     // When index is 0, divide sum computed so    // far by n.    if (i == 0)        return ((a[i] + avgRec(a, i+1, n))/n);     // Compute sum    return (a[i] + avgRec(a, i+1, n));} // Function that return average of an array.double average(int a[], int n){     return avgRec(a, 0, n);} // Driver codeint main(){    int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};    int n = sizeof(arr)/sizeof(arr[0]);     cout << average(arr, n) << endl;    return 0;}
// Java program to calculate average// of array elements class CalcAvg{    // Recursively computes average of a[]    static double avgRec(int a[], int i, int n)    {        // Last element        if (i == n-1)            return a[i];              // When index is 0, divide sum computed so        // far by n.        if (i == 0)            return ((a[i] + avgRec(a, i+1, n))/n);              // Compute sum        return (a[i] + avgRec(a, i+1, n));    }          // Function that return average of an array.    static double average(int a[], int n)    {         return avgRec(a, 0, n);    }         // main function    public static void main (String[] args)    {        int arr[] = {10, 2, 3, 4, 5, 6, 7, 8, 9};        int n = arr.length;              System.out.println(average(arr, n));    }}
# Python3 code to calculate# average of array elements # Recursively computes average of a[]def avgRec( a , i , n ):         # Last element    if i == n-1:        return a[i]         # When index is 0, divide sum    # computed so far by n.    if i == 0:        return ((a[i] + avgRec(a, i+1, n)) / n)         # Compute sum    return (a[i] + avgRec(a, i+1, n)) # Function that return# average of an array.def average(a, n):    return avgRec(a, 0, n) # Driver codearr = [10, 2, 3, 4, 5, 6, 7, 8, 9]n = len(arr)print(average(arr, n)) # This code is contributed by "Sharad_Bhardwaj".
// C# program to calculate average// of array elementsusing System; class GFG {         // Recursively computes average of a[]    static double avgRec(int []a, int i, int n)    {                 // Last element        if (i == n-1)            return a[i];             // When index is 0, divide sum        // computed so far by n.        if (i == 0)            return ((a[i] + avgRec(a, i+1, n))/n);             // Compute sum        return (a[i] + avgRec(a, i+1, n));    }         // Function that return average of an array.    static double average(int []a, int n)    {        return avgRec(a, 0, n);    }         // main function    public static void Main ()    {        int []arr = {10, 2, 3, 4, 5, 6, 7, 8, 9};        int n = arr.Length;             Console.Write(average(arr, n));    }} // This code is contributed by Smitha.
<?php// PHP program to calculate// average of array elements // Recursively computes// average of a[]function avgRec($a, $i, $n){    // Last element    if ($i == $n - 1)        return $a[$i];     // When index is 0, divide    // sum computed so far by n.    if ($i == 0)        return (($a[$i] +                 avgRec($a, $i +                         1, $n)) / $n);     // Compute sum    return ($a[$i] +            avgRec($a, $i + 1, $n));} // Function that return// average of an array.function average($a, $n){    return avgRec($a, 0, $n);} // Driver code$arr = array(10, 2, 3, 4,             5, 6, 7, 8, 9);$n = sizeof($arr); echo average($arr, $n) ; // This code is contributed by Anuj_67?>
<script> // Java script  program to calculate average// of array elements // Recursively computes average of a[]function avgRec(a, i, n){         // Last element    if (i == n - 1)        return a[i];     // When index is 0, divide sum computed so    // far by n.    if (i == 0)        return ((a[i] + avgRec(a, i + 1, n)) / n);     // Compute sum    return (a[i] + avgRec(a, i + 1, n));} // Function that return average of an array.function average(a, n){    return avgRec(a, 0, n);} // Driver codelet arr = [ 10, 2, 3, 4, 5, 6, 7, 8, 9 ];let n = arr.length; document.write(average(arr, n)); // This code is contributed by sravan kumar G </script>
6
Related Article : Average of a stream of numbers
This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Smitha Dinesh Semwal
nitin mittal
vt_m
olafgaspriggi
rdtank
sravankumar8128
simmytarika5
rishavmahato348
CBSE - Class 11
school-programming
Arrays
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Introduction to Arrays
Given an array A[] and a number x, check for pair in A[] with sum as x (aka Two Sum)
Python | Using 2D arrays/lists the right way
Subset Sum Problem | DP-25
Introduction to Data Structures