Given an array of integers return the sum of the values stored in the first and last index of the array. The array will have at least 2 elements in it
---------------------------------------
public class Class1 {
public static int endSum(int[] values) {
//Enter code here
}
}
Please find below code and don't forget to give a Like.
Code: Change the below class name Main to your class name
public class Main
{
public static void main(String[] args) {
int total;
int[] arr={1,2,3,4};
if(arr.length>=2){
total=endSum(arr);
System.out.println("Total of element at first and last
index is:"+total);
}
else{
System.out.println("Array should have atleast 2
elements");
}
}
public static int endSum(int[] values){
int total=0;
total=values[0]+values[values.length-1];
return total;
}
}
Output and screenshot of code:
Get Answers For Free
Most questions answered within 1 hours.