public class HackerEarth
{
public static void main(String[] args)
{
int[] values = new int[3];
values[0] = 10;
values[1] = new Integer(5);
values [2] = 15;
for(int i=1; i<values.length; i++)
{
System.out.println(values[i]-values[i-1]);
}
}
Answers
Output:
-5
10
Explanation:
Program:
public class HackerEarth
{
public static void main(String[] args)
{
int[] values = new int[3];
values[0] = 10;
values[1] = new Integer(5);
values [2] = 15;
for(int i=1; i<values.length; i++)
{
System.out.println(values[i]-values[i-1]);
}
}
}
An array named 'values' of type Integer and size -> 3 is taken.
At index 0, the value 10 is inserted. At 1 and 2, values 5 and 15 are inserted in the respective positions.
So, the array 'values' of size 3 contains the elements:
At index 1 : 10
At index 2: 5
At index 3: 15
A loop has started with 1 and ends less than the size of the array 'values' which is 3 [excludes]. So, the iteration goes for 2 times with iterable values 1, 2.
With i=1, values[1] - values[1-1] = values[1] - values[0] = 5-10 = -5
With i=2, values[2] - values[2-1] = values[2] - values[1] = 15-5 = 10
Loop terminates!
Therefore, the output will be:
-5
10
Learn more:
1. Write a program to check whether it is a Lead number or not in java
https://brainly.in/question/15644815
2. Write a program in Java to generate the following pattern using nested loops.
https://brainly.in/question/18819254