-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampleTest3_program06.java
64 lines (56 loc) · 1.68 KB
/
sampleTest3_program06.java
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// This code was contributed by https://github.com/voyager2005
import java.util.Scanner;
public class Program6
{
public static void main(String args[])
{
// declaration
Scanner sc = new Scanner(System.in);
int[] A, B;
int n;
int beginIndex, endIndex;
// prompt and accept the length of the array
System.out.println("Please enter the length of the array");
n = sc.nextInt();
// initialization
beginIndex = 0 ;
endIndex = (n-1) ;
// initializing the array A
A = new int[(n)];
B = new int[(n)];
// initializing each of the array elements of array A
for(int i = 0 ; i < n ; i++)
{
System.out.println("Please enter number " + (i+1));
A[i] = sc.nextInt();
}
// computation
for( int i = 0 ; i < n ; i++)
{
if( A[i] % 2 == 0)
{
B[beginIndex] = A[i];
beginIndex++;
}
else if( A[i] % 2 != 0)
{
B[endIndex] = A[i];
endIndex--;
}
}
// displaying array A
System.out.println();
System.out.println("This is Array A");
for( int i = 0 ; i < n ; i++)
{
System.out.println("element " + (i+1) + " : " + A[i]);
}
// displaying array B
System.out.println();
System.out.println("This is Array B");
for( int i = 0 ; i < n ; i++)
{
System.out.println("element " + (i+1) + " : " + B[i]);
}
}
}