In this article we will show you, How to Write a Program to Print Fibonacci Series in C programming language using While Loop, For Loop, Functions and Recursion.

## Fibonacci Series

In Mathematics, Fibonacci Series or Fibonacci Numbers are the numbers that are displayed in following sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 …

If you observe the above pattern, First Value is 0, Second Value is 1, and the subsequent number is the result of sum of the previous two numbers. For example, Third value is (0 + 1), Fourth value is (1 + 1) so on and so forth.

## C Fibonacci Series program using While Loop

This C Program allows the user to enter any positive integer and display the Fibonacci series of number from 0 to user specified number using the While Loop

/* C Fibonacci series Program using While Loop */ #include <stdio.h> int main() { int Number, i = 0, Next, First_Value = 0, Second_Value = 1; printf("\n Please Enter the Range Number: "); scanf("%d",&Number); while(i < Number) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } printf("%d \t", Next); i++; } return 0; }

**OUTPUT**

**ANALYSIS**

First, We declared 4 integer variables Number, i, First_Value, Second_Value, and assigned values as we shown above. Following statements will ask the user to enter any positive integer and then, that number is assigned to Number variable.

printf("\n Please Enter the Range Number: ");

scanf(“%d”,&Number);

Below While loop will make sure that, the loop will start from 0 and it is less than the user specified number. Within the While loop, we used If Else statement.

- If i value is less than or equal to 1, i value will be assigned to Next.
- If i value is greater than 1, perform calculations inside the Else block.

while(i < Number) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } printf("%d \t", Next); i++; }

Let us see the working principle of this while loop in iteration wise. From the above screenshot you can observe that, User entered values: Number = 5, i = 0, First_Value = 0, Second_Value = 1

**First Iteration**

- The condition inside the While (0 < 5) returns TRUE so, program will start executing the statements inside the while loop
- Within the while loop we have If Else statement and the condition
= 1) returns TRUE so Next = 0 and, compiler will exit from if statement block**if (0 <** - Print statement
**printf(“%d”, Next)**will print the value 0 - Lastly, i will be incremented to 1. Please refer Increment and Decrement Operators in C article to understand the ++ notation.

**Second Iteration**

- The condition inside the While (1 < 5) returns TRUE so, program will start executing the statements inside the loop
- Within the while loop, If condition
= 1) returns TRUE so Next = 1 and compiler will exit from if statement block**if (1 <** - Print statement
**printf(“%d”, Next)**will print the value 1 - Lastly, i will be incremented to 1

**Third Iteration**

- The condition inside the While (2 < 5) returns TRUE so, program will start executing the statements inside the while loop.
- Within the loop, If condition
returns FALSE so statements inside the else block will start executing**if (2 <= 1)**- Next = First_Value + Second_Value ==> 0 + 1 = 1
- First_Value = Second_Value = 1
- Second_Value = Next = 1

- Next, Print statement
**printf(“%d”, Next)**will print the value 1. - Lastly, i will be incremented to 1

**Fourth Iteration**

- The condition inside the While (3 < 5) returns TRUE so, program will execute the statements inside the while loop.
- Within the loop, we have the If condition
returns FALSE so statements inside the else block will start executing**if (3 <= 1)**- Next = First_Value + Second_Value ==> 1 + 1 = 2
- First_Value = Second_Value = 1
- Second_Value = Next = 2

- Next, Print statement
**printf(“%d”, Next)**will print the value 2. Lastly, i will be incremented to 1

**Fifth Iteration**

- The condition inside the While (4 < 5) returns TRUE so, compiler will start executing the statements inside the loop.
- Within the while loop we have the If condition
= 1) returns FALSE so compiler will start executing the statements inside the else block**if (4 <**- Next = First_Value + Second_Value ==> 1 + 2 = 3
- First_Value = Second_Value = 2
- Second_Value = Next = 3

- Next, Print statement
**printf(“%d”, Next)**will print the value 3. - Lastly, i will be incremented to 1

**Sixth Iteration**

While (5 < 5) returns FALSE so, program will exit from the while loop.

From the above, Our final output of Next values are: **0 1 1 2 3**

## C Fibonacci Series Program using For Loop

This C Program allows the user to enter any positive integer and then, this program will print the Fibonacci series of number from 0 to user specified number using For Loop

/* C Fibonacci series Program using For Loop */ #include <stdio.h> int main() { int Number, Next, i, First_Value = 0, Second_Value = 1; printf("\n Please Enter the Range Number: "); scanf("%d",&Number); /* Find & Displaying Fibonacci series */ for(i = 0; i <= Number; i++) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } printf("%d \t", Next); } return 0; }

**OUTPUT**

We just replaced the While loop in the above example with the For loop. Please refer For Loop article here For Loop to understand the for loop functionality.

## Use Functions for C Fibonacci Series Program

This C Program allows the user to enter any positive integer, and print the Fibonacci series of number from 0 to user specified number using Functions

/* C Fibonacci Series Program using Functions */ #include<stdio.h> void Fibonacci_series(int Number) ; int main() { int Number; printf("Enter the number of terms\n"); scanf("%d", &Number); printf("Fibonacci series First %d Numbers:\n", Number); Fibonacci_series(Number) ; return 0; } void Fibonacci_series(int Number) { int i, First_Value = 0, Second_Value = 1, Next; for(i = 0; i <= Number; i++) { if(i <= 1) { Next = i; } else { Next = First_Value + Second_Value; First_Value = Second_Value; Second_Value = Next; } printf("%d\t", Next); } }

**OUTPUT**

**ANALYSIS**

When the compiler reaches to * Fibonacci_series(Number);* line then the compiler will immediately jump to below function:

void Fibonacci_series(int Number)

We already explained LOGIC above example.

**NOTE: **If we create a function with Void then there is no need to return any value but, if we declared a function with any data type (int, float etc) then we have return something out from the function.

## C Program to Print Fibonacci Series using Recursion

This C Program program allows the user to enter any positive integer, and display the Fibonacci series of number from 0 to user specified number using Recursion concept.

/* C Fibonacci series Program using Recurssion */ #include<stdio.h> int Fibonacci_Series(int); int main() { int Number, i = 0, j; printf("\n Please Enter Number upto which you want too: "); scanf("%d", &Number); printf("Fibonacci series\n"); for ( j = 0 ; j <= Number ; j++ ) { printf("%d\t", Fibonacci_Series(j)); } return 0; } int Fibonacci_Series(int Number) { if ( Number == 0 ) return 0; else if ( Number == 1 ) return 1; else return ( Fibonacci_Series(Number - 1) + Fibonacci_Series(Number - 2) ); }

**OUTPUT**

**ANALYSIS**

**Fibonacci Class Analysis:**

In this class, we defined a function. Following function will accept integer values as parameter value and return integer value

int Fibonacci_Series(int Number)

Lets see the Else If statement inside the above specified functions

- if (Number == 0) will check, whether the given number is 0 or not. If it is TRUE, function will return the value Zero.
- if (Number == 1) will check, whether the specified number equal to 1 or not. If it is TRUE, function will return the value One.
- If the number is greater than 1, compiler will execute the statements inside the else block.

Within the Else block we are calling the Fibonacci_Series function recursively to display the Fibonacci numbers.

return ( Fibonacci_Series(Number - 1) + Fibonacci_Series(Number - 2) );

For example, Num = 2

- (Fibonacci_series(Num – 2) + Fibonacci_series(Num – 1))
- (Fibonacci_series(2 – 2) + Fibonacci_series(2 – 1)), It means
- (Fibonacci_series (0)+ Fibonacci_series(1))
- return (0 + 1) = return 1

**NOTE:** For Recursive functions it is very important to place a condition before using the function recursively otherwise, we will end up in infinite execution (Same like infinite Loop).

Please be careful ðŸ™‚

Thank you for Visiting Our Blog

## Leave a Reply