Write a java program to find alternate sum and difference of a given number


Program
import java.util.*; public class AltDiff { public static void main(String[] args) { Scanner sc=new Scanner(System.in); int sum=0; int n=sc.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) { a[i]=sc.nextInt(); } for(int i=0;i<n/2;i++) { sum+=Math.abs(a[i]-a[n-i-1]); } System.out.println(sum); } }
Input
Output