Advertisement
CoineTre

JF-LabArrays07.Condense Array to Number

Jan 18th, 2021
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.14 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Lab7CondenseArrayToNumber {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String input = scanner.nextLine();
  7.         String[] inputNumbers = input.split(" ");
  8.         int[] arrayIntegers = new int[inputNumbers.length];
  9.         for (int i = 0; i < arrayIntegers.length; i++) {
  10.             arrayIntegers[i]= Integer.parseInt(inputNumbers[i]);
  11.         }
  12.         while (arrayIntegers.length>1){
  13.             int[] arr = new int[arrayIntegers.length-1];
  14.             for (int i = 0; i < arrayIntegers.length-1 ; i++) {
  15.                arr[i]= arrayIntegers[i] + arrayIntegers[i+1];
  16.             }
  17.             arrayIntegers=arr;
  18.         }
  19.         System.out.println(arrayIntegers[0]);
  20.  
  21.     }
  22. }
  23.  
  24. /*
  25. * Write a program to read an array of integers and condense them by summing adjacent couples
  26. *  of elements until a single integer is obtained. For example, if we have 3 elements {2, 10, 3},
  27. *  we sum the first two and the second two elements and obtain {2+10, 10+3} = {12, 13}, then we sum again
  28. *  all adjacent elements and obtain {12+13} = {25}.
  29. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement