Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2 , the first 10 terms will be: By considering the terms in the Fibonacci sequence whose values do not exceed N , find the sum of the even-valued terms.
MY SOLUTION .
import java.util.Scanner;
public class FIBONACCI_SUM {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int T=input.nextInt();
while(T>0) {
int N=input.nextInt();
int a=1,b=2,i,ans=0,sum=0,count=0;
for(i=1;i<=N;i++) {
if(a<=N) {
sum=a+b;
a=b;
b=sum;
if(a%2==0) {
ans=ans+a;
if(ans<=N) {
count=ans;
}
}
else {
}
}
else {
break;
}
}
System.out.println(count);
T--;
}
}
}