java - Recursively sum of an array -


i have program i'm trying make class adding array integer 2 100 increment of 2 , using recursion. answer should summation of 2 100 increment of 2 (2+4+6+....+100);

here program looks far:

public class arraysindepth { public static void main(string[]args){ int size, sum;       int[] array = new int [101];  sum = sum(array,2);    system.out.println("the sum of numbers " +sum); }      public static int sum(int[] arr,int index){   int sumofnumbers=0;     arr[index] = index; sumofnumbers = sumofnumbers + arr[index];   enter code here   if(index<arr.length) {      sumofnumbers = sum(arr,index);    index += 2;    return sumofnumbers;   }  else{ return 0;  }  }   } 

but getting error every time. , can not run it. answer should summation of 2 100 increment of 2 (2+4+6+....+100); please me someone. in lot of trouble. thank you.

the way now, returning value arr[index] @ every depth, without summing anything. algorithm should this:

public int sum(int[] arr, int index) {     if(/*index still valid*/) {         return arr[index] + sum(arr, index+2);     } else {         return 0;     } } 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -