-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion141_MakeLast.java
More file actions
46 lines (33 loc) · 1.11 KB
/
Question141_MakeLast.java
File metadata and controls
46 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package week7_Array_2D_ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class Question141_MakeLast {
public static void main(String[] args) {
// olcay // Jul 26, 2020
/* Given an int array, print a new array with double the length where its last element is the same as the original array, and all the other elements are 0. The original array will be length 1 or more. Note: by default, a new int array contains all 0's.
Example:
input: 4 5 6
output: [0, 0, 0, 0, 0, 6]
Example:
input: 0
output: [0, 0]
Example:
input: 1 2 3 4
output: [0, 0, 0, 0, 0, 0, 0, 4]
*/
Scanner scan = new Scanner(System.in);
System.out.println("Enter the array size: ");
int size=scan.nextInt();
int[] array = new int[size];
for(int i=0;i<size;i++) {
System.out.println("Enter number " + (i+1));
array[i]=scan.nextInt();
}
int[] newArray = new int[size*2];
for(int i=0;i<newArray.length-1;i++) {
newArray[i]=0;
}
newArray[newArray.length-1]=array[array.length-1];
System.out.println(Arrays.toString(newArray));
}
}