Fibonacci Sequence Generator in C

C Program to generate Fibonacci sequence.


Problem

Write a program to generate the first n numbers of the Fibonacci sequence using a loop of your choice.

Concept

The Fibonacci sequence is a sequence in which each number is obtained by adding the previous two numbers. For example, if we want to know the 7th Fibonacci number in the sequence, then we need to add the 6th and the 5th Fibonacci number.

So, Fib(7) = Fib(6) + Fib(5)

In the same way,

Fib(n) = Fib(n-1) + Fib(n-2)

Following are some of initial numbers in the Fibonacci sequence:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …

Note: we are assuming that the Fibonacci sequence starts from the zeroth term.

Example 1:

Input:
Enter the number:  8

Output:
0, 1, 1, 2, 3, 5, 8, 13 

Example 2:

Input:
Enter the number:  0

Output:
No output

Expectations:

You need to write the complete program using the following template as the starter:

#include <stdio.h>

int main()
{
    int n;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    // Write your code here.

    return 0;
}

Copy and paste the above code in your favorite IDE and start writing your code after the comment “// Write your code here.”

Pro tip:  First, try writing the code on your own. Try every possible thing you can to come up with the solution. If you still can’t figure it out, then check the solution and see what went wrong. This is how you learn. 🙂

Sample Test Cases:

To verify the program you have written, go ahead and test your program using the following test cases:

InputOutput
Test Case 10No output
Test Case 2 10
Test Case 350, 1, 1, 2, 3
Test Case 4-10n cannot be negative

Don’t forget to try the code on your own before diving into the solution.


Solution

The Fibonacci sequence generator is a famous problem in programming. Following is the solution to this problem:

#include <stdio.h>

int main()
{
    int n;
    
    printf("Enter a number: ");
    scanf("%d", &n);
    
    int first = 0, second = 1, next;
    
    if (n < 0) {
        printf("n cannot be negative.");
        exit(1); // exit the program with failure
    }
    
    printf("Fibonacci Sequence: ");
    if (n >= 0) printf("%d", first);
    if (n >= 1) printf(", %d", second);
    
    for (int i = 2; i < n; i++) {
        next = first + second;
        first = second;
        second = next;
        printf(", %d", next);
    }
    
    printf("\n");

    return 0;
}

Let’s analyze the above program line by line:

  • The header files are declared at the top for standard input/output operations (stdio.h) and exit operation (stdlib.h)
  • Within the main() function, three variables are declared:
    • The variable “first” indicates the first value of the Fibonacci sequence.
    • The variable “second” indicates the second value of the Fibonacci sequence. 
    • The variable “next” indicates the next value in the Fibonacci sequence obtained after adding the previous terms in the loop. 
    • These variables are initialized to first and second values of the Fibonacci sequence. Later, they will be added in the loop.
  • The first if condition checks of n is negative. If yes, then the appropriate message will be displayed and we exit from the program with FAILURE code 1. The exit() function is available in the stdlib.h header file.
  • The if check “if (n >= 0) printf(“%d”, first);” displays 0 if n is greater than or equal to 0. For all n values equal to or greater than 0, value 0 must be displayed.
  • Similarly, the if check “if (n >= 1) printf(“, %d”, second);” displays 1 if n is greater than or equal to 1. 
  • The for loop runs from the 3rd value (i = 2) of the Fibonacci sequence till the nth value. 
  • When i = 2
    • The 3rd term is calculated by adding previous two terms i.e. 0 and 1. This results 1. Therefore, next = 1.
    • Then first becomes 1.
    • And second becomes 1.
    • The value of next is displayed which is the 3rd value of the Fibonacci sequence which is 1. 
  • When i = 3
    • The 4th term is calculated by adding previous two terms i.e. 1 and 1. This results 2. Therefore, next = 2.
    • Then first becomes 1.
    • And second becomes 2.
    • The value of next is displayed which is the 4th value of the Fibonacci sequence which is 2. 
  • In the same way, the loop displays first n numbers of the Fibonacci sequence.


Leave a comment

Leave a comment

Your email address will not be published. Required fields are marked *

Thank you for choosing to leave a comment. Please be aware that all comments are moderated in accordance with our policy. For more information, please review our comments policy. Rest assured that your email address will not be shared with anyone. Kindly refrain from using keywords in your comment. Let’s maintain a respectful atmosphere and engage in meaningful conversations.