TheBulgarianWolf

Condense Array To Number

Apr 26th, 2020
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.82 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main
  3. {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.         System.out.print("Enter n= ");
  7.         int n = Integer.parseInt(sc.nextLine());
  8.         int[] array = new int[n];
  9.         for(int i =0;i < array.length;i++){
  10.             System.out.print("Element[" + i + "] = " );
  11.             array[i] = Integer.parseInt(sc.nextLine());
  12.         }
  13.         //Condensing an array of integers by summing adjacent couples of elements until a single number is obtained
  14.         int[] condensed = new int[n-1];
  15.         while(n > 1){
  16.             condensed = new int[n-1];
  17.             for(int i = 0;i<condensed.length;i++){
  18.                 condensed[i] = array[i] + array[i+1];
  19.                 array[i] = condensed[i];
  20.             }
  21.             n--;
  22.         }
  23.        
  24.         System.out.println(condensed[0]);
  25.     }
  26. }
Add Comment
Please, Sign In to add comment