Tutorial Gateway

  • C
  • C#
  • Java
  • Python
  • SQL
  • MySQL
  • Js
  • BI Tools
    • Informatica
    • Talend
    • Tableau
    • Power BI
    • SSIS
    • SSRS
    • SSAS
    • MDX
    • R Tutorial
    • Alteryx
    • QlikView
  • More
    • C Programs
    • C++ Programs
    • Python Programs
    • Java Programs

C Fibonacci Series Program

by suresh

How to Write a Program to Print Fibonacci Series in C programming using While Loop, For Loop, Functions, and Recursion?.

Fibonacci Series in C

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

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

Fibonacci series in C using While Loop

This Program allows the user to enter any positive integer. And then, display the Fibonacci series of number from 0 to user-specified number using the While Loop in C programming.

/* 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;
}
C Fibonacci Series Program 1

In this Fibonacci series in c program, first, We declared four integer variables Number, i, First_Value, Second_Value, and assigned values as we showed above. The following statements ask the user to enter any positive integer, and it assigns to a Number variable.

printf("\n Please Enter the Range Number: ");
scanf("%d",&Number);

The While loop in this c Fibonacci series program 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 of Fibonacci series in c in iteration wise. From the above C programming screenshot, you can observe that the user entered values are Number = 5, i = 0, First_Value = 0, Second_Value = 1

C Fibonacci First Iteration

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

C Fibonacci Second Iteration

  • The condition While (1 < 5) returns TRUE 
  • If condition if (1 <= 1) returns TRUE. So, Next = 1, and the compiler will exit from if statement block
  • printf(“%d”, Next) will print the value 1
  • Lastly, i will be incremented to 1

Third Iteration

  • While (2 < 5) returns TRUE
  • if (2 <= 1) returns FALSE. So, statements inside the else block will start executing 
    • 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.
  • i incremented by 1

C Fibonacci Fourth Iteration

  • While (3 < 5) returns TRUE
  •  if (3 <= 1) returns FALSE. So, the compiler execute else block
    • Next = First_Value + Second_Value ==> 1 + 1 = 2
    • First_Value and Second_Value = 1
    • Second_Value = Next = 2 
  • Next, printf(“%d”, Next) prints the value 2. Next, i wil be 4

Fifth Iteration: while(4 < 5) = True

  • If condition if (4 <= 1) returns FALSE 
    • Next = 1 + 2 => 3
    • First_Value = Second_Value = 2
    • Second_Value = Next = 3 
  • Next, printf(“%d”, Next) prints 3. Now, i becomes 5

Sixth Iteration: While (5 < 5) returns FALSE. So, the compiler exits from the while loop.

From the above, Our final output of C Fibonacci Series program 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 C program will print the Fibonacci series of number from 0 to n 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;
}
C Fibonacci Series Program 2

We just replaced the While loop in the above Fibonacci series in c example with the For loop. Please refer to the For Loop in C article.

Fibonacci series in C using Functions

This C program print Fibonacci series of number from 0 to 100 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);
   }
}
C Fibonacci Series Program 3

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

void Fibonacci_series(int Number)

Fibonacci series in C using Recursion

This Program in C prints the Fibonacci series numbers from 0 to user-specified value using the Recursion.

/* C Fibonacci series Program using Recursion */
#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) );
}
C Fibonacci Series Program 4

C Fibonacci Class Analysis:

The following Fibonacci series function will accept integer values as parameter value and return an integer value

int Fibonacci_Series(int Number)

Let’s see the Else If statement inside the above-specified functions

  • if (Number == 0) checks whether the given number is 0 or not. When it is TRUE, the function will return the value Zero.
  • if (Number == 1) checks number equal to 1 or not. If it’s TRUE, the function returns One.
  • and if the number is greater than 1, the compiler executes the statements in the else block.

Within the Else block of this C fibonacci series using Recursion program, 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 the Fibonacci series using the recursive function, 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).

Placed Under: C Programs

  • C Hello World
  • C Add Two numbers
  • C Armstrong Number Program
  • C Convert Celsius to Fahrenheit
  • C Convert Decimal to Binary
  • C Convert Decimal to Octal
  • C Convert CM to Meter and KM
  • C Convert KM to M, CM & MM
  • C Convert Fahrenheit to Celsius
  • C Compound Interest Program
  • C Count Digits in a Number
  • C Count Notes in an Amount
  • C Cube of a Number
  • C Electricity Bill Program
  • C Factors of a Number Program
  • C Factorial of a Number Program
  • C GCD of Two Numbers
  • C Generic Root of a Number
  • C Gross Salary of an Employee
  • C Largest of Two Numbers
  • C Largest of Three numbers
  • C Last Digit Of a Number
  • C LCM of Two Numbers
  • C Leap Year program
  • C Multiplication Table program
  • C Natural Numbers from 1 to N
  • C Natural Numbers in reverse
  • C NCR Factorial of a Number
  • C Number is Divisible by 5 and 11
  • C Palindrome Program
  • C Palindrome String program
  • C Perfect Number Program
  • C Profit or Loss Program
  • C Print 1 to 100
  • C Print Prime Numbers 1 to 100
  • C Prime Number program
  • C Prime Factors of a Number
  • C Prime, Armstrong or Perfect
  • C Positive or Negative program
  • C Print Odd Numbers 1 to N
  • C Print Even Numbers 1 to N
  • C Print Integer, Char & Float
  • C Power of a Number program
  • C Product of Digits in a Number
  • C Roots of a Quadratic Equation
  • C Reverse a Number
  • C Simple Calculator Program
  • C Simple Interest
  • C Square of a Number
  • C Square Root of a Number
  • C Standard Deviation program
  • C Strong Number Program
  • C Student Grade program
  • C Sum of Odd Numbers program
  • C Sum of Even Nums program
  • C Sum of Even and Odd Nums
  • C Swap First & Last digit of Num
  • C Sum of First & Last digit Num
  • C Sum of Digits of a Number
  • C Sum of N Numbers Program
  • C Sum and Average of n Number
  • C Swap 2 Numbers
  • C Total, Avg & % of Five Subjects
  • C Quick Sort
  • C Selection Sort
  • C Insertion Sort
  • C Bubble Sort
  • C Fibonacci Series Program
  • C Nth Fibonacci number
  • C Sum of AP Series
  • C Sum of GP Series
  • C Sum of series 1³+2³+3³+….+n³
  • C Sum of series 1²+2²+3²+….+n²
  • C ASCII value of String chars
  • C Print Characters in a String
  • C Compare Two Strings
  • C Concatenate Two Strings
  • C Copy String
  • C String length program
  • C Convert String to Lowercase
  • C First Occurrence of string char
  • C First Occur of String Word
  • C Count Occur of string Char
  • C Count Alphabets, Digits & Special Chars
  • C Count Vowels & Consonants
  • C string remove all Occ of char
  • C remove 1st Occ of char in string
  • C remove last Occ char in string
  • C replace All Occ of char in string
  • C Replace last Occ of String Char
  • C Replace 1st occ char in string
  • C Reverse words Order in string
  • C Reverse a String
  • C Toggle Case of all char in string
  • C Array Arithmetic Operations
  • C Matrix Arithmetic Operations
  • C Count Frequency of array item
  • C Count Duplicate Array items
  • C Count +ve & -Ve Array items
  • C Count Even & Odd Array items
  • C Copy an Array to another
  • C Delete Duplicate Array items
  • C Delete an Element in an Array
  • C insert an Element in an Array
  • C 2nd largest Number in an Array
  • C Find Largest Array Number
  • C Find Smallest Array Number
  • C Largest & Smallest Array item
  • C Merge Two Arrays
  • C Print Unique Array Elements
  • C Print Arrayy Elements
  • C Print Negative Array Numbers
  • C Print Positive Array Numbers
  • C Put +ve & -Ve in 2 Arrays
  • C Put Even & Odd in 2 Arrays
  • C Program to Reverse an Array
  • C Search an Element in an Array
  • C Sum of array even, odd nums
  • C Sort Array in Ascending Order
  • C Sort Array in Descending
  • C Swap 2 Arrays without Temp
  • C Sum of all Array Elements
  • C Sum of each Matrix column
  • C sum of each row in a Matrix
  • C Sum of Matrix row & column
  • C Add Two Matrices
  • C Sparse Matrix
  • C Symmetric Matrix
  • C Identity Matrix
  • C Interchange Matrix Diagonals
  • C Check two Matrices Equal
  • C Lower Triangle of a Matrix
  • C Upper Triangle of a Matrix
  • C Sum of Lower Triangle Matrix
  • C Sum of Upper Triangle Matrix
  • C Subtract Two Matrices
  • C Transpose of a Matrix program
  • C Cuboid Volume & Surface Area
  • C Cone Volume & Surface Area
  • C Cube Volume & Surface Area
  • C Volume & Surface of Cylinder
  • C Volume & Surface of Sphere
  • C Tutorial
  • C# Tutorial
  • Java Tutorial
  • JavaScript Tutorial
  • Python Tutorial
  • MySQL Tutorial
  • SQL Server Tutorial
  • R Tutorial
  • Power BI Tutorial
  • Tableau Tutorial
  • SSIS Tutorial
  • SSRS Tutorial
  • Informatica Tutorial
  • Talend Tutorial
  • C Programs
  • C++ Programs
  • Java Programs
  • Python Programs
  • MDX Tutorial
  • SSAS Tutorial
  • QlikView Tutorial

Copyright © 2021 | Tutorial Gateway· All Rights Reserved by Suresh

Home | About Us | Contact Us | Privacy Policy