Java while loop find even fibonacci numbers

Description

Java while loop find even fibonacci numbers

public class Main {
  public static void main(String[] args) {
    int maxNumber = Integer.MAX_VALUE/2;
    int a = 1;//from ww w .j a  v a2  s.  c  om
    int b = 2;
    int c = 2;
    long answer = 2;
    while (c <= maxNumber) {
      c = a + b;
      a = b;
      b = c;
      if (c <= maxNumber && (c % 2) == 0) {
        answer += c;
        System.out.println(answer);  
      }     
    }    
  }
}



PreviousNext

Related