C Programming Examples – Fibonacci Series Using Loop Statement
Topic: C Loop Statement
Problem: Create a program that displays the Fibonacci sequence of a user input. More about Fibonacci here.
Sample Output:
Enter a Number: 7 Fibonacci Series: 1 1 2 3 5 8 13
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.
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.
This process will continue until we reach the number of digits the user inputs.
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.
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.
Have you seen the pattern? Do we have a more specific steps now? Haven’t seen it? Take a look again!
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?
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?
- We started the process by adding 1(which is the first digit) to zero. These 2 numbers are determined beforehand. After that,
- We take the sum of the two numbers.
- 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.
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?
Related posts:
- C Programming Examples – Factorial using Loop Statement
- C Programming Examples – Averaging Using Loop Statement
- C Programming Examples – Displaying Even Numbers using Loop Statement
- C Programming Tutorial 2 – Intro to Variables
- 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 activity, Fibonacci, sequence. Bookmark the permalink.












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