C PROGRAMMING PROGRAM • LOOPS & NUMBER PROBLEMS

Print the Fibonacci Series

Learn how to print the fibonacci series using a clear C program.

BeginnerSequenceLoopState update

PROBLEM UNDERSTANDING

Input and expected output

Sample input
8
Sample output
0 1 1 2 3 5 8 13

COMPLETE C PROGRAM

Complete C implementation

fibonacci-series.c
Open in compiler
#include <stdio.h>

int main(void)
{
    int terms;
    unsigned long long first = 0, second = 1;

    printf("Enter number of terms: ");
    scanf("%d", &terms);
    if (terms < 1 || terms > 94) {
        printf("Use a term count from 1 to 94.\n");
        return 0;
    }
    for (int index = 0; index < terms; index++) {
        printf("%llu%c", first, index == terms - 1 ? '\n' : ' ');
        unsigned long long next = first + second;
        first = second;
        second = next;
    }
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

0 1 1 2 3 5 8 13
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Print the current term, then shift the pair forward using their sum.
  3. Display the computed result.

Print the current term, then shift the pair forward using their sum.

EFFICIENCY

Time and space complexity

Time complexity

O(n)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.