We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Most easy solution in Java & it passes all the test cases:
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
long first = 0, second = 1, sum =0, sumeven =0;
while(second <= n){
sum = first+second;
first = second;
second = sum;
if(sum%2==0 && sum<n){
sumeven += sum;
}
}
System.out.println(sumeven);
}
}
Project Euler #2: Even Fibonacci numbers
You are viewing a single comment's thread. Return to all comments →
I've just copied my code from C++ and changed it to Java (used long instead of long long int) and it works like a charm!
Same worked for me! thanks
It's working for me also, But what's the logic behind this?
Most easy solution in Java & it passes all the test cases:
}
https://www.hackerrank.com/contests/projecteuler/challenges/euler002/forum/comments/1109726