C Program to generate Fibonacci sequence

What is Fibonacci sequence?

Fibonacci sequence is series in which each successive number is the sum of the previous two numbers. For example:

The following is a C program to generate Fibonacci sequence based on the number of terms entered by the user.

How it works

The following table demonstrates what happens at each iteration of the loop (assuming terms = 5)

Iteration   z x y i
After 1st iteration   z = x + y = 0 + 1 = 1 x = y = 1   y = z = 1    i = 2
After 2nd iteration   z = 1 + 1 = 2 x = 1   y = 2    i = 3
After 3rd iteration   z = 1 + 2 = 3 x = 2   y = 3    i = 4
After 4th iteration   z = 2 + 3 = 5 x = 3   y = 5    i = 5

Recommended Reading:

Leave a Comment

%d bloggers like this: