C Programming Examples – Fibonacci Series Using Loop Statement


Difficulty: Beginner
Topic: C Loop Statement

Problem: Create a program that displays the Fibonacci sequence of a user input. More about Fibonacci here.

VN:F [1.9.11_1134]
Rating: 5.0/5 (1 vote cast)

Sample Output:

Enter a Number: 7

Fibonacci Series: 1 1 2 3 5 8 13
Fibonacci Sequence In C

Figure 9.0 - Fibonacci Sequence in C


Solution:

First let’s learn about the formula of a Fibonacci sequence. In Fibonacci sequence, each new number is the sum of the first 2 numbers. Take a look at Figure 1.0.

The Fibonacci sequence

Figure 1.0 - Fibonacci Process

As you see in the figure above, we started from adding 1 to zero giving us the 3rd number in sequence. After that we proceed on adding the last 2 numbers to get the 4th number.

Fibonacci Sequence

Figure 2.0 - Fibonacci Process

This process will continue until we reach the number of digits the user inputs.

Fibonacci Sequence

Figure 3.0 - Fibonacci Pattern

The last sum of the Fibonacci Sequence is the last digit. Take a look on Figure 4.0, it shows how we count our sequence.

Fibonacci Sequence

Figure 4.0 - Ending Fibonacci Sequence

Now let’s further analyze the problem. We have to find a way to translate this problem into a process that our compiler can understand. We can first try to separate each process into a series of steps. How are we going to do that? Simply separate each operations.

Fibonacci Sequence

Figure 5.0 - Fibonacci Operations

Have you seen the pattern? Do we have a more specific steps now? Haven’t seen it? Take a look again!

Fibonacci Pattern

Figure 6.0 - The Fibonacci Pattern

In every step, the sum and the last number of the the addends were moved to the left making the sum as the other addends. Can we now distinguish what variables we need?

Fibonacci variables

Figure 7.0 - Fibonacci Variables

From this pattern we can now declare our variables. The first addends will be add1 and the second addend will be add2. What else do we need? We need another variable for the sum. Now that we have identified what variable we need, we can now proceed on identifying the program flow.

Take a look again on Figure 1.0. How did we start the process?

  1. We started the process by adding 1(which is the first digit) to zero. These 2 numbers are determined beforehand. After that,
  2. We take the sum of the two numbers.
  3. Then we moved the sum to the addends removing the first addend. Then repeat step 2.

Now we have our loop. How about determining the end of the process? When should the loop of the process ends? The process ends when the Nth number is reached. Meaning, if the user inputs 7 we stop at 7th digit, or the 6th sum. Let’s take a look at the following Flow Chart.

Fibonacci Flow Chart

Figure 8.0 - Fibonacci Flow chart

Maybe some of you noticed and asking, why is the condition is “is ctr < num?” and not “is ctr <= num?”. It is because we have already declare the first digit which is the number 1. All we need now is the 6 sums or num – 1.

The code will look like this:

#include<stdio.h>
main()
{
      // Declare the variables we are going to need
      int ctr, num, add1, add2, sum;
 
      // Initiate the values for add1 and add2
      add1 = 0;
      add2 = 1;
 
      //Ask a number from user
      printf("Enter Number:");
      scanf("%d", &num);
 
      //print the initial values of add1 and add2
      printf("%d %d ", add1, add2);
      //Start the loop
      for (ctr = 1; ctr < num; ctr++)
          {
               //compute for sum
               sum = add1 + add2;
               //print the numbers
               printf("%d ", sum);
               //reassign the values of add2 and sum to add1 and add2 respectively
               add1 = add2;
               add2 = sum;
               }
      getch();
      return 0;
      }

Self Activity: What if the user Entered 0 or 1?

C Programming Examples – Fibonacci Series Using Loop Statement, 5.0 out of 5 based on 1 rating

Related posts:

  1. C Programming Examples – Factorial using Loop Statement
  2. C Programming Examples – Averaging Using Loop Statement
  3. C Programming Examples – Displaying Even Numbers using Loop Statement
  4. C Programming Tutorial 2 – Intro to Variables
  5. C Programming Examples – Simple Averaging

If you have any questions please feel free to discuss it on our discussion board

www.itechsociety.com/forum/topic/c-programming-activity-fibonacci-series-using-loop-statement

This entry was posted in C/C#/C++, Examples, Programming, Tutorials and tagged , , . Bookmark the permalink.
This article is written by: chicoi08
I love making great things using my computer. From programming to designing to blogging. I enjoy doing them. I hope this website can help some people out there. View all posts by chicoi08 →

Discussion 1 Comment

  1. hemant says:

    I like it to learn…….thanks for thise…

    VA:F [1.9.11_1134]
    Rating: +1 (from 1 vote)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">