Java Program to print the sum of the even terms of a fibonacci series till 4 million


 

// Program by Akash Tripathi (@proakash256)
  
import java.util.Scanner;
public class Fibonacci_even_Sum
{
    public static void main(String args[])
    {
        int a = 0 , b = 1 , sum = a + b , s = 0 , n;
        Scanner sc = new Scanner(System.in);
        System.out.printf("Enter the number of terms
                           till which you want to
                           print the sum : ");
        n = sc.nextInt();
        sc.close();
        for(int i = 1i <= ni = i + 1)
        {
            if(sum >= 4000000)
                break;
            if(sum % 2 == 0)
                s = s + sum;
            a = b;
            b = sum;
            sum = a + b;
        }
        System.out.printf("%d" , s);
    }
}

Comments