EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Fibonacci Series in C

Home » Software Development » Software Development Tutorials » C Programming Tutorial » Fibonacci Series in C

Fibonacci Series in C

Introduction to Fibonacci Series in C

In the Fibonacci Series in C, a number of the series is the result of the addition of the last two numbers of the series. C program with a loop and recursion for the Fibonacci Series. You can print as many series terms as needed using the code below. The Fibonacci numbers are referred to as the numbers of that sequence. The series ‘ first number is 0, 1, 2, 3, 5, 8,… Each other word is the sum of the two preceding terms with the exception of the first two sequence terms, such as 10 = 2 + 8 (addition of the 2 and 8).

The Fn of Fibonacci numbers are described by the recurrence relationship in mathematical terms

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Fn = Fn-1 + Fn-2

Fibonacci Series Program in C without Recursion:

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 2; i < number; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
return 0;
}

Output:

Fibonacci Series in C

 Fibonacci Series using Recursion in C:

#include<stdio.h>
#include<conio.h>
void printFibonacci(int number)
{
static int first_number = 0, second_number = 1, third_number;
if(number > 0)
{
third_number = first_number + second_number;
first_number = second_number;
second_number = third_number;
printf("%d ",third_number);
printFibonacci(number - 1);
}
}
int main()
{
int number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number: \n");
printf("%d %d ", 0, 1);
printFibonacci(number - 2); //number-2 is used because we have already print 2 numbers
return 0;
}

Output:

Fibonacci Series in C

Fibonacci Series in C using a For Loop

In the For loop, the Initialization step is executed and only once in the whole program. In this step, you can initialize and declare variables for the code. Then the condition will get evaluated.

If the condition is true then it will execute the code inside the block of For loop. If the condition is false then it will jump to the code after the For loop without executing the code of For loop.

Popular Course in this category
C Programming Training (3 Courses, 5 Project)3 Online Courses | 5 Hands-on Projects | 34+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,617 ratings)
Course Price

View Course

Related Courses
C++ Training (4 Courses, 5 Projects, 4 Quizzes)Java Training (40 Courses, 29 Projects, 4 Quizzes)

After the For loop, the increment statement will be executed. After that again the condition will be checked. Loop will get executed if the condition is true and the loop will repeat itself i.e. the body of the loop, an increment statement, and condition. The For loop ends when the condition is false.

Program to Generate Fibonacci Series using For Loop:

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 0; i < number; i++) //loop will starts from 2 because we have printed 0 and 1 before
{
if(i <= 1)
third_number = i;
else
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
}
return 0;
}

Output:

Fibonacci Series in C

Fibonacci Series Using While Loop

In the While loop, Base on Condition, While loop gets executed multiple times.

If the condition is true then it will execute the code inside the block of While loop. If the condition is false then it will jump to the code after the While loop without executing the code of While loop. So let’s see how we can generate the Fibonacci series Using While Loop.

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number = 0, i = 3, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
while(i <= number)
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
i = i + 1;
}
return 0;
}

Output:

 Fibonacci Series in C

Fibonacci Series in C using an Array:

Let f(n) be the n’th term.

f(0)=0;

f(1)=1;

f(n)=f(n-1)+f(n-2); (for n>=2)

Series will be as Follows:

0

1

0 + 1 = 1

1 +1 = 2

1 + 2 = 3

2 + 3 = 5

3 + 5 = 8

5 + 8 = 13

8 + 13 = 21

13 + 21 = 34

21 + 34 = 55

…and so on

Program to Generate Fibonacci Series using Array:

#include<stdio.h>
#include<conio.h>
int main()
{
int fibonacci[25], i, number;
printf("Enter the number for fibonacci series:");
scanf("%d",&number);
fibonacci[0] = 0;
fibonacci[1] = 1;
for (i = 2; i < number; i++)
{
fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
}
printf("Fibonacci Series for a given number: \n");
for (i = 0; i < number; i++)
{
printf("%d ", fibonacci[i]);
}
return 0;
}

Output:

Fibo Series

Fibonacci Series using Specified Number

The first two numbers are 0 and 1, and the other numbers in the series are generated by adding the last two numbers of the series using looping. These numbers are stored in an array and will be printed as output.

Program to Generate Fibonacci Series using Specified Number:

#include<stdio.h>
#include<conio.h>
int main()
{
int first_number = 0, second_number = 1, third_number, i;
printf("Fibonacci Series for a given number:");
printf("\n%d %d", first_number, second_number); //To print 0 and 1
for(i = 2; i < 10; ++i) //loop will starts from 2 because we have printed 0 and 1 before
{
third_number = first_number + second_number;
printf(" %d", third_number);
first_number = second_number;
second_number = third_number;
}
return 0;
}

Output:

Fibo Series

Conclusion

In this article, we have seen how to generate the Fibonacci series in C by various methods. I hope you’ll find this article helpful.

Recommended Articles

This is a guide to Fibonacci Series in C. Here we discuss the introduction to the Fibonacci series, how to use  For Loop, While Loop and Specified Number along with some sample code. You may also look at the following articles to learn more –

  1. C Command
  2. Fibonacci Series in C++
  3. Fibonacci Series in C#
  4. Fibonacci Series in Java

C Programming Training (3 Courses, 5 Project)

3 Online Courses

5 Hands-on Projects

34+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
C Programming Tutorial
  • C programs
    • Patterns in C Programming
    • Star Patterns in C
    • Number Patterns in C
    • Swapping in C
    • Reverse Number in C
    • Palindrome in C Program
    • Factorial in C
    • Fibonacci Series in C
    • Square Root in C
    • Random Number Generator in C
    • Prime Numbers in C
    • Escape Sequence in C
    • Reverse String in C
    • Leap Year Program in C
    • Anagram Program in C
    • Strong Number in C
    • String Concatenation in C
    • C Programming Matrix Multiplication
    • Decimal to Octal in C
    • Expression Evaluation in C
    • Decimal to Hexadecimal in C
  • Basic
    • Introduction to C
    • What is C
    • Career in C Programming
    • Advantages of C
    • How to Install C
    • Best C Compilers
    • Data Types in C
    • Variables in C
    • C Keywords
    • C Command
    • Command Line Arguments in C
    • C Literals
    • Constants in C
    • Unsigned Int in C
    • String in C
  • Pointers
    • Pointers in C
    • Null pointer in C
    • Function Pointer in C
    • Double Pointer in C
    • Void Pointer in C
    • Const Pointer in C
    • Dangling Pointers in C
    • Pointer Arithmetic in C
  • Operators
    • C Operators
    • Arithmetic Operators in C
    • Relational Operators in C
    • Assignment Operators in C
    • Logical Operators in C
    • Conditional Operator in C
    • Modulus Operator in C
    • Ternary Operator in C
    • Address Operator in C
    • Unary Operator in C
    • Operators Precedence in C
    • Left Shift Operator in C
  • Control Statement
    • Control Statements in C
    • If Statement in C
    • If-else Statement in C
    • Else if Statement in C
    • Nested if Statement in C
    • #else in C
    • Structure Padding in C
    • Nested Structure in C
    • Continue Statement in C
    • Break Statement in C
    • Switch Statement in C
    • Goto Statement in C
  • Loops
    • Loops in C
    • For Loop in C
    • While Loop in C
    • Do While Loop in C
    • Nested Loop in C
    • Infinite Loop in C 
  • Function
    • Math Functions in C
    • Hashing Function in C
    • Recursive Function in C
    • Power Function in C
    • fputs in C
    • C puts() Function
    • fprintf() in C
    • fseek() in C
    • Stderr in C
    • ASCII Value in C
    • strcat() in C
    • Inline Function in C
    • sizeof() in C
    • Function Prototype in C
    • C ftell()
  • Array
    • Arrays in C Programming
    • 2-D Arrays in C
    • 3D Arrays in C
    • Multidimensional Array in C
    • Array Functions in C
    • Strings Array in C
  • Sorting
    • Sorting in C
    • Heap Sort in C
  • Advanced
    • Constructor in C
    • Encapsulation in C
    • C Storage Classes
    • Static Keyword in C
    • File Handling in C
    • Queue in C
    • Hexadecimal in C 
    • typedef in C
    • Memory Allocation in C
    • Linked List in C
    • Volatile in C
    • Tokens in C
    • Expression in C
    • Regular Expression in C
    • Error Handling in C
    • Types of Errors in C
    • Preprocessor in C
    • Preprocessor Directives in C
    • fscanf() in C
    • #Pragma in C
    • #ifndef in C
    • #undef in C
    • Macros in C
  • Interview question
    • C Programming Interview Questions

Related Courses

C Programming Training Course

C++ Training Course

Java Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C Programming Training (3 Courses, 5 Project) Learn More