-
Notifications
You must be signed in to change notification settings - Fork 6
/
Two arrays game.java
58 lines (49 loc) · 1.23 KB
/
Two arrays game.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
import java.util.Scanner;
public class Main{
public static int[] Calculate(int[] a, int[] b, int Size){
int[] finalArray=new int[Size];
for ( int i=0; i<Size; i++){
finalArray[i]=a[i]+b[i];
i++;
}
for ( int j=1; j<Size; j++){
finalArray[j]=a[j]-b[j];
j++;
}
return finalArray;
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
System.out.println("Enter the size for the first array");
int Size=sc.nextInt();
if(Size<=0){
System.out.println("Invalid array size");
System.exit(1);
}
System.out.println("Enter the elements for the first array");
int[] farray=new int[Size];
for ( int i=0; i<Size; i++){
farray[i]=sc.nextInt();
}
System.out.println("Enter the size for the second array");
int Size2=sc.nextInt();
if(Size2<=0){
System.out.println("Invalid array size");
System.exit(1);
}
if(Size!=Size2){
System.out.println("Both array size are not same");
System.exit(1);
}
System.out.println("Enter the elements for the second array");
int[] sarray=new int[Size2];
for ( int i=0; i<Size2; i++){
sarray[i]=sc.nextInt();
}
int[] x=Calculate(farray,sarray,Size);
System.out.println("The elements of the third array");
for (int i=0; i<x.length; i++){
System.out.println(x[i]);
}
}
}