title: Finding Average of Array of integers using recursion
body: <p>I'm trying to find the average of integers elements in an array using <em>recursion</em>. I know how to do it using loops, but I have to do it by recursion for my assignment, so what I tried to do is to find the sum of elements using recursion and then divide the sum by the length of the array. I wrote this code but it gives me a wrong result:</p>
  
  <pre><code>public int findAvg(int a[], int n)
  {
  int sum,avg;
  if(n==1)
   {
  
  sum=a[0];
  return sum;
  }
  else 
  {
  sum=a[n-1]+findAvg(a,n-1);
  }
  
  avg = sum/n;
  return avg;}
  </code></pre>
  
  <hr>
  
  <p>The calling of findAvg method in main class:</p>
  
  <pre><code>public class main {
  
  public static void main(String[] args) {
      // TODO Auto-generated method stub
  
      Recursive r = new Recursive ();
      int integersArr [] = {1,2,3,4,5};
  
      int max = r.findMax(integersArr,integersArr.length );
      int avg = r.findAvg(integersArr, integersArr.length);
      System.out.println("Maximum element = "+ max);
      System.out.println("Average value of elements = "+ avg);
  
  
   }
  
  }
  </code></pre>
  
  <p>The console output:</p>
  
  <blockquote>
    <p>Average value of elements = 1</p>
  </blockquote>