-
Notifications
You must be signed in to change notification settings - Fork 0
/
ST06Excersise.java
56 lines (43 loc) · 1.23 KB
/
ST06Excersise.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
package com.ysingh.structure;
import java.util.Arrays;
import java.util.List;
public class ST06Excersise {
public static void main(String args[]) {
List<Integer> numbers = Arrays.asList(12,9,13,5,4,2,4,12,15);
System.out.println("Square Each Number and Print Sum:");
int sum = squareNumbersInListAndPrintSum(numbers);
System.out.println(sum);
System.out.println();
System.out.println("Cube Each Number and Print Sum:");
sum = cubeNumbersInListAndPrintSum(numbers);
System.out.println(sum);
System.out.println();
System.out.println("Sum of Odd Numbers in the list:");
sum = sumOfOddNumbersInList(numbers);
System.out.println(sum);
System.out.println();
}
private static int squareNumbersInListAndPrintSum(List<Integer> numbers) {
int sum = 0;
for(int number : numbers) {
sum = sum + (number * number);
}
return sum;
}
private static int cubeNumbersInListAndPrintSum(List<Integer> numbers) {
int sum = 0;
for(int number : numbers) {
sum = sum + (number * number * number);
}
return sum;
}
private static int sumOfOddNumbersInList(List<Integer> numbers) {
int sum = 0;
for(int number : numbers) {
if(number % 2 != 0) {
sum = sum + number;
}
}
return sum;
}
}