3

 avatar
unknown
plain_text
3 years ago
1.2 kB
3
Indexable
using System;

namespace FibonacciSum
{
    class Program
    {
        static long Fibonacci(long n)
        {
            if (n <= 1)
            {
                return n;
            }

            long a = 0;
            long b = 1;
            for (long i = 2; i <= n; i++)
            {
                long c = a + b;
                a = b;
                b = c;
            }
            return b;
        }

        static void Main(string[] args)
        {
            long n = 10; // Calculate the 10th term in the Fibonacci sequence
            long y = 4000000000; // Sum up to 4,000,000,000
            long fibonacci = Fibonacci(n);
            Console.WriteLine("The " + n + "th term in the Fibonacci sequence is " + fibonacci);

            long sum = 0;
            long a = 1;
            long b = 2;
            while (b < y)
            {
                if (b % 2 == 0)
                {
                    sum += b;
                }
                long c = a + b;
                a = b;
                b = c;
            }
            Console.WriteLine("Sum of all even numbers in the Fibonacci sequence up to " + y + ": " + sum);
        }
    }
}
Editor is loading...