© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•4y ago•
126 replies
yatta

Fibonacci in recursive ?

So I have made 2 versions of Fibonacci calculator, one is in normal way and one is in recursive
    public static int Fib(int n)
    {
        if (n == 0) { return 0; }
        if(n == 1) { return 1; }
        int a = 0;
        int b = 1;
        int res = 0;
        for (int i = 0; i <= n-1; i++)
        {
            res = a + b;
            a = b;
            b = res;

        }
        return res;
    }

    public static int FibRec(int n)
    {
        if((n == 0) || (n == 1)) { return n; }
        else
        {
            return FibRec(n-1) + FibRec(n-2);
        }
    }
    public static int Fib(int n)
    {
        if (n == 0) { return 0; }
        if(n == 1) { return 1; }
        int a = 0;
        int b = 1;
        int res = 0;
        for (int i = 0; i <= n-1; i++)
        {
            res = a + b;
            a = b;
            b = res;

        }
        return res;
    }

    public static int FibRec(int n)
    {
        if((n == 0) || (n == 1)) { return n; }
        else
        {
            return FibRec(n-1) + FibRec(n-2);
        }
    }

When i run both the same time, the recursive version is incorrect. I tried to check for a correct version ones on the internet but strangely all the answer is quite the same as mine. I'm very weak at recursive and I absolutely have no idea what wrong with, so I'd be very grateful if any expert can point out the problem.
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

✅ Fibonacci sequence
C#CC# / help
2y ago
✅ Fibonacci sequence
C#CC# / help
3y ago
❔ recursive calls result in stackoverflow
C#CC# / help
3y ago
❔ Recursive Multiplier
C#CC# / help
3y ago