-
Notifications
You must be signed in to change notification settings - Fork 93
/
SumOfEvenAndOddNumbersinString.java
52 lines (44 loc) · 1.27 KB
/
SumOfEvenAndOddNumbersinString.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
import java.util.Scanner;
public class SumOfEvenAndOddNUmbersInString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of an array");
int len = sc.nextInt();
int arry[] = new int[len];
System.out.println("Enter array elements ");
for (int i = 0; i < len; i++) {
arry[i] = sc.nextInt();
}
System.out.println("Sum of All numbers are " + SumOfAllNumber(arry, len));
System.out.println("Sum of even numbers are " + SumOfEvenNumber(arry, len));
System.out.println("Sum of odd numbers are " + SumOfOddNumber(arry, len));
}
// sum of all numbers in an array
private static int SumOfAllNumber(int[] arry, int len) {
int sum = 0;
for (int i = 0; i < len; i++) {
sum = sum + arry[i];
}
return sum;
}
// sum of Odd numbers in an array
private static int SumOfOddNumber(int[] arry, int len) {
int s_num = 0;
for (int i = 0; i < len; i++) {
if (arry[i] % 2 == 1V) {
s_num += arry[i];
}
}
return s_num;
}
// sum of Even numbers in an array
private static int SumOfEvenNumber(int[] arry, int len) {
int s_num = 0;
for (int i = 0; i < len; i++) {
if (arry[i] % 2 == 0) {
s_num += arry[i];
}
}
return s_num;
}
}