-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEvenOddArrayElements.java
More file actions
49 lines (45 loc) · 1.37 KB
/
Copy pathEvenOddArrayElements.java
File metadata and controls
49 lines (45 loc) · 1.37 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
47
48
49
import java.util.Scanner;
public class EvenOddArrayElements {
static int[] fetch(int a[]){
return a;
}
//PRINTING EVEN ELEMENTS OF ARRAY AND THEIR SUM
static void evenElements(int a[]){
int sum=0;
System.out.print("Even numbers: ");
for(int i=0; i<a.length; i++){
if(a[i]%2==0){
System.out.print(a[i] + " ");
sum+=a[i];
}
}
System.out.println("\nSum of even numbers: " + sum);
}
//PRINTING ODD ELEMENTS OF ARRAY AND THEIR SUM
static void oddElements(int a[]){
int sum=0;
System.out.print("Odd numbers: ");
for(int i=0; i<a.length; i++){
if(a[i]%2==1){
System.out.print(a[i] + " ");
sum+=a[i];
}
}
System.out.println("\nSum of odd numbers: " + sum);
}
public static void main(String args[]){
Scanner sc= new Scanner(System.in);
//ARRAY SIZE AND ELEMENTS INPUT
System.out.print("Enter size of array: ");
int size= sc.nextInt();
int arr[]= new int[size];
System.out.print("Enter array elements: ");
for(int i=0; i<size; i++){
arr[i]= sc.nextInt();
}
//METHODS CALLED TO FIND EVEN AND ODD ELEMENTS
int a[]= fetch(arr);
evenElements(a);
oddElements(a);
}
}