completed even fibonacci numbers

This commit is contained in:
aj 2019-02-08 15:59:26 +00:00
parent 8e77b319b3
commit 96b7f93332

View File

@ -0,0 +1,30 @@
package evenfibonaccinumbers;
import java.util.ArrayList;
import java.util.List;
public class Fibonacci {
public static void main(String[] args) {
int upperBound = 4000000;
int total = 2;
List<Integer> fibonacci = new ArrayList<Integer>();
fibonacci.add(1);
fibonacci.add(2);
int next;
while(fibonacci.get(fibonacci.size() - 1) < upperBound) {
next = fibonacci.get(fibonacci.size() - 2) + fibonacci.get(fibonacci.size() - 1);
fibonacci.add(next);
if(next % 2 == 0)
total += next;
}
System.out.println("The total of even fibonacci numbers below " + upperBound + " is " + total);
}
}