Dark mode logo
Last Updated:
Display fibonacci series till n term in c

C program to display fibonacci series

Algorithm

Input: Input a numbers
output: Displays the fibonacci series till the input term

  1. Start
  2. Input a numbers to display fibonacci series till that term say n
  3. t1 = 0
  4. t2 = 1
  5. sum = t1 + t2
  6. if ( n == 1)
    6.1 print t1
  7. else if (n == 2)
    7.1 print t1 , t2
  8. else 
    8.1 print t1 , t2
    8.2 for (i = 3; i <= n; i++)
      8.2.1 print sum
      8.2.2 t1 = t2
      8.2.3 t2 = sum
      8.2.4 sum = t1 + t2
  9. stop

C Program

#include <stdio.h>
int main( )
{
int n, t1 = 0, t2 = 1, sum = t1+t2;
printf("Enter a number to display the fibonacci: ");
scanf("%d", &n);
if ( n == 1) {
    printf("%d",t1);
    }
else if ( n == 2) {
           printf("%d %d", t1 ,t2);
           }
else {
        printf("%d %d ", t1 , t2);
        for(int i = 3 ; i <= n; i++) {
             printf("%d ", sum);
             t1 = t2;
             t2 = sum;
             sum = t1 + t2;
} }
}


Note: This Program was created using visual studio code. The Program file can be downloaded by clicking here

Explanation

Starting with the declaration, I declared a variable n to store the nth term value. Then for the first and second terms, I declared a variable t1 = 0 and t2 =1. This is done because when considering n = 1, we can display directly 0 in the console without checking a condition and in the case of n =2, we can display 0 1 in the console also without checking any condition. This is done to reduce the complexity of the program.

Then I declared another variable sum = t1 + t2. The use of this will be explained down below. Then as with any regular program, we input a number using the standard input function, i.e, printf function and the value is stored in n using the scanf function. Then as explained above we check if the value of n is 1, if yes then we display 0 in the console and exit the program. If the value of n is 2, then we display 0 1 in the console and exit the program.

When the value of n is 3 or greater we first print 0 and 1 without checking any condition in the console. You may ask why? This is done because we are writing the code to display after 3 digits in the Fibonacci series only. So to compensate for that, we are including 0 1 at the beginning of the program without checking any condition. Then we use for loop to display each term in the Fibonacci series. for (i = 3 ; i <=n; i++), while this condition is true we print sum. Now, this section might be a little tricky, so put on your thinking cap😀. 

Remember in the beginning we stored sum = t1 + t2. This is done so that we can print the value of the sum, which is 1 once before entering the loop. Then we set t1 = t2 and t2 = sum and sum = t1 + t2. Here the sum is given inside a loop, and this sum displays the values of each Fibonacci term and runs till the value of i < = n runs false. After that, the program exits and we get the Fibonacci series in the console window.

FlowChart

Note: This Flowchart was created using a program called Raptor. The flowchart file can be downloaded by clicking here

Input
n:10

Output
0 1 1 2 3 5 8 13 21

Explanation

There is a hiccup while comparing the logic of the algorithm and implementing it to the flowchart in raptor. For ease of understanding, I'll start from the beginning. First, we get input from the user to determine how many terms of the Fibonacci series we should display. Then, if it's 1. We display 0 in the output window without even checking any condition. If the input value is 2, we display 0 1 in the output window, that too even without checking any condition. If the input value is greater than 2, i.e 3 or above we print 0 and 1 like earlier then we assign sum = t1 + t2 where t1 has a value of 0 and t2 has a value of 1. 

The problem in logic occurs here, cause we don't have anything to substitute for for loop in raptor. We only have if, i.e represented by rhombus. So think of it like this, In for loop the condition was like i <= n, but when it comes to the flowchart we must convert this logic to an if statement. So in for loop it was like this process should run till i < = n. When converting it to an if statement it will become like if i > = n do the next statement of programs. 

Comments