Interview Question Categories

SUM OF FIBONACCI SEQUENCE

SUM OF FIBONACCI SEQUENCE


namespace SumOfFibSequence
{
    class Program
    {
        static void Main(string[] args)
        {
            int x = 0;
            int y = 1;
            int n = 5;

            if (n == 0)
            {
                Console.WriteLine("0");
                return;
            }
            else if (n == 1)
            {
                Console.WriteLine("1");
                return;
            }

            int sum = 1;
            int z = 0;
            for (int i = 2; i < n; i++)
            {
                z = x + y;
                sum += z;
                x = y;
                y = z;  
            }
            Console.WriteLine(sum);
        }
    }
}

No comments:

Post a Comment