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.
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(); // Read the limit N for this test case
long sum = 0;
// Starting the Fibonacci sequence
long a = 1, b = 2;
// Calculate the sum of even Fibonacci numbers not exceeding N
while (b <= n) {
if (b % 2 == 0) {
sum += b;
}
// Generate the next Fibonacci number
long next = a + b;
a = b;
b = next;
}
// Output the result for this test case
System.out.println(sum);
}
in.close();
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Project Euler #2: Even Fibonacci numbers
You are viewing a single comment's thread. Return to all comments →
import java.io.; import java.util.; import java.text.; import java.math.; import java.util.regex.*;
public class Solution {
}