Alternate Number differences in Java

Question :

Sample input 1 :

Element:
4
5
6
7
4-6+5-7

Sample output 1 :

4

Sample input 2 :

Element:
9
13
14
15
9-14+13-15

Sample output 2 :

7

Code :

package sda;
import java.util.*;
public class AlterNateSumDiff {
	public static void main(String[] args) {
	Scanner sc=new Scanner(System.in);
	int no=sc.nextInt();
	int arr[]=new int[no];
	int diff=0,sum=0;
		for(int i=0;i<no;i++) {
			arr[i]=sc.nextInt();
		}
		for(int i=0;i<no/2;i++) {
			diff=arr[i]-arr[no-i-1];
			sum+=diff;
		}
	System.out.println(sum);
	}
}

Recommended:

  1. Java Program of Kids School Example
  2. Java Program of Java – Python Count
  3. Java Program of Hy-Fly Taxi
  4. Java Program for Harima Realtor’s Example
  5. Numerology Name Checking
  6. OMR Evaluation
  7. Java program to Sum of Even Elements

Leave a Comment

Your email address will not be published. Required fields are marked *