Get Fibonacci - CSharp System

CSharp examples for System:Math Geometry

Description

Get Fibonacci

Demo Code


using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;/*from w  w w .  j a  v a  2s . c  om*/

public class Main{
        internal static int GetFibonacci(int n)
        {
            if (n < 1)
            {
                return -1;
            }
            if (n >= Int_Fibo_MAX_FOR_APP)
            {
                return -1;
            }

            int f1 = 1, f2 = 1, f = 1;
            for (int i = 2; i < n; i++)
            {
                f = f1 + f2;
                f1 = f2;
                f2 = f;
            }
            return f >= Int_Fibo_MAX_FOR_APP ? Int_Fibo_MAX_FOR_APP : f;
        }
}

Related Tutorials