-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumEvenNumbers.java
More file actions
36 lines (29 loc) · 830 Bytes
/
Copy pathSumEvenNumbers.java
File metadata and controls
36 lines (29 loc) · 830 Bytes
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
public class SumEvenNumbers {
public static void main(String[] args) {
System.out.println("Total sum: ${returnSumOfEvensinRange(1, 200)}" ); ;
}
public static int returnSumOfEvensinRange(int start, int evensToAdd){
int sum = 0;
int index = start;
int count = 0;
while(true){
if(isEven(index)){
System.out.println("Found an even number " + index + " adding to sum!");
sum += index;;
count++;
}
index++;
if(count >= evensToAdd){
break;
}
}
return sum;
}
public static boolean isEven(int num){
if(num % 2 == 0){
return true;
}else{
return false;
}
}
}