Answer:
The Java code is given below
Step-by-step explanation:
import java.util.*;
public class AlternatingSum
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] nums = line.split(" ");
int[] intArray = new int[nums.length];
for(int i=0;i<nums.length;i++)
{
intArray[i] = Integer.parseInt(nums[i]);
}
alternatingSum(intArray);
}
public static void alternatingSum(int[] array)
{
int sum = 0;
for(int i=0;i<array.length;i++)
{
if(i%2 == 0)
{
sum+= array[i];
if(i!=0)
System.out.print(" + "+array[i]);
else
System.out.print(array[i]);
}
else
{
sum -= array[i];
System.out.print(" - "+array[i]);
}
}
System.out.println(" = "+sum);
}
}