Calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21, - CSharp Data Structure Algorithm

CSharp examples for Data Structure Algorithm:Algorithm

Description

Calculates the sum of the first N members of the sequence of Fibonacci: 0, 1, 1, 2, 3, 5, 8, 13, 21,

Demo Code

using System;/*from  w  w w  . j  a va  2s. com*/
using System.Numerics;

class Fibonacci
{
    static void Main()
    {
        int n = int.Parse(Console.ReadLine());
        BigInteger temp = 0;
        BigInteger firstNum = 0;
        BigInteger secondNum = 1;
        BigInteger sum = 0;
        if (n == 0 || n == 1)
        {
            Console.WriteLine(0);
        }
        else
        {
            for (int i = 0; i < (n - 2); i++)
            {
                temp = firstNum + secondNum;
                firstNum = secondNum;
                secondNum = temp;
                sum += temp;
            }
            Console.WriteLine(1 + sum);
        }
    }
}

Related Tutorials