Search code examples
cfibonacci

How to print fibonacci series upto 50 terms in c?


I want to print the Fibonacci series up to 50 terms but when the n is 50 it's not working it works until n <= 48. Is there any way to do that?

#include <stdio.h>

int main()
{
    int n;
    scanf("%d", &n);
    unsigned long long arr[50] = {0, 1};

    for (int i = 0, j = 2; i < n; i++, j++)
    {
        arr[j] = arr[i] + arr[i + 1];
    }

    for (int i = 0; i < n; i++)
    {
        printf("%d => %llu\n", i, arr[i]);
    }

    return 0;
}

Solution

  • well the problem is solved I did mistakes in the loop condition so here is the updated code.

    #include <stdio.h>
    
    int main()
    {
        int n;
        scanf("%d", &n);
        unsigned long long arr[50] = {0, 1};
    
        for (int i = 2; i < n; i++)
        {
            arr[i] = arr[i - 1] + arr[i - 2];
        }
    
        for (int i = 0; i < n; i++)
        {
            printf("%llu ", arr[i]);
        }
    
        return 0;
    }