public class Array
{
public static int array( int[] arr, int first, int last)
{
// int sum = 0;
if(arr[first] == arr[last])/* must be if(first == last),but try this one too, study the code, it is interesting */
{
return arr[first];
}
else
{
return arr[first] + array(arr, first+1, last);
}
}
}
//main class
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the input you want to enter: ");
int size = input.nextInt();
int[] numArr = new int[size];
System.out.print("Enter "+ size +" numbers: ");
for(int i=0; i<numArr.length; i++)
{
numArr[i]=input.nextInt();
}
System.out.print("The sum of the numbers is: "+ Array.array(numArr, 0 , size-1) );
}
}
0 comments:
Post a Comment