Mean of array using recursion - GeeksforGeeks

29 Jul, 2021
To find the mean of the elements of the array.  
Mean = (Sum of elements of the Array) /
       (Total no of elements in Array)
Examples:  
Input : 1 2 3 4 5
Output : 3

Input : 1 2 3
Output : 2
 
To find the mean using recursion assume that the problem is already solved for N-1 ie you have to find for n 
Sum of first N-1 elements = 
                 (Mean of N-1 elements)*(N-1)

Mean of N elements = (Sum of first N-1 elements + 
                      N-th elements) / (N)
Note : Since array indexing starts from 0, we access N-th element using A[N-1]. 
 
C++
C
Java
Python3
C#
PHP
Javascript
// Recursive C++ program to find mean of array#include <iostream>using namespace std; // Function Definition of findMean functionfloat findMean(int A[], int N){    if (N == 1)        return (float)A[N-1];    else        return ((float)(findMean(A, N-1)*(N-1) +                                    A[N-1]) / N);} // Main Calling Functionint main(){    float Mean = 0;    int A[] = {1, 2, 3, 4, 5};    int N = sizeof(A)/sizeof(A[0]);    cout << " "<< findMean(A, N);    return 0;} // this code is contributed by shivanisinghss2110
// Recursive C program to find mean of array#include<stdio.h> // Function Definition of findMean functionfloat findMean(int A[], int N){    if (N == 1)        return (float)A[N-1];    else        return ((float)(findMean(A, N-1)*(N-1) +                                    A[N-1]) / N);} // Main Calling Functionint main(){    float Mean = 0;    int A[] = {1, 2, 3, 4, 5};    int N = sizeof(A)/sizeof(A[0]);    printf("%.2f\n", findMean(A, N));    return 0;}
// Recursive Java program to find mean of arrayclass CalcMean{    // Function Definition of findMean function    static float findMean(int A[], int N)    {        if (N == 1)            return (float)A[N-1];        else            return ((float)(findMean(A, N-1)*(N-1) +                                        A[N-1]) / N);    }         // main Function    public static void main (String[] args)    {        float Mean = 0;        int A[] = {1, 2, 3, 4, 5};        int N = A.length;        System.out.println(findMean(A, N));    }}
# Recursive Python3 program to# find mean of array # Function Definition of findMean functiondef findMean(A, N):     if (N == 1):        return A[N - 1]    else:        return ((findMean(A, N - 1) *                (N - 1) + A[N - 1]) / N) # Driver CodeMean = 0A = [1, 2, 3, 4, 5]N = len(A)print(findMean(A, N)) # This code is contributed by Anant Agarwal.
// Recursive C# program to find mean of arrayusing System; class CalcMean{    // Function Definition of findMean function    static float findMean(int []A, int N)    {        if (N == 1)            return (float)A[N - 1];        else            return ((float)(findMean(A, N - 1) *                           (N - 1) + A[N - 1]) / N);    }          // Driver code    public static void Main()    {        //float Mean = 0;        int []A = {1, 2, 3, 4, 5};        int N = A.Length;        Console.WriteLine(findMean(A, N));    }} // This code is contributed by Anant Agarwal.
<?php// Recursive PHP program to find mean of array // Function Definition of findMean functionfunction findMean($A, $N){    if ($N == 1)        return $A[$N - 1];    else        return ((findMean($A, $N - 1) * ($N - 1) +                          $A[$N - 1]) / $N);} // Driver Code$Mean = 0;$A = array(1, 2, 3, 4, 5);$N = sizeof($A);echo findMean($A, $N); // This code is contributed by ajit.?>
<script>    // Recursive Javascript program to find mean of array         // Function Definition of findMean function    function findMean(A, N)    {        if (N == 1)            return A[N - 1];        else            return ((findMean(A, N - 1) * (N - 1) + A[N - 1]) / N);    }         // float Mean = 0;    let A = [1, 2, 3, 4, 5];    let N = A.length;    document.write(findMean(A, N));         // This code is contributed by rameshtravel07.</script>
Output:  
3
This article is contributed by Prakhar Agrawal. 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. 
jit_t
rameshtravel07
surindertarika1234
shivanisinghss2110
Arrays
School Programming
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Stack Data Structure (Introduction and Program)
Multidimensional Arrays in Java
Queue | Set 1 (Introduction and Array Implementation)
Linear Search
Python | Using 2D arrays/lists the right way
Python Dictionary
Inheritance in C++
Reverse a string in Java
Overriding in Java
Copy Constructor in C++