# Fibonacci Series in C/C++ with Explanation.

## Fibonacci Series in C/C++ with Explanation.

The Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. For e.g. 0 1 1 2 3 5 8 ….. is a fibonacci series. Let’s see the program to find fibonacci series in C and C++ understand the concept.

Eg. Write a program in C/C++ to find Fibonacci series.

## 1. Program in C:

``````//Fibonacci series in C
#include<stdio.h>
int main()
{
int num,n1=0,n2=1,n3,count=2;
printf("\n Enter limit for fibonacci series : ");
scanf("%d",&num);
printf("\n Fibonacci series of %d numbers is \n",num);
printf("%d\t%d",n1,n2);
while(count<num)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
count++;
printf("\t%d",n3);
}
return 0;
}
``````

## 2.Program in C++:

``````//Fibonacci series in C++
#include<iostream>
using namespace std;
int main()
{
int num,n1=0,n2=1,n3,count=2;
cout<<"\n Enter limit for fibonacci series : ";
cin>>num;
cout<<"\n Fibonacci series of "<<num<<" numbers is \n";
cout<<n1<<"\t"<<n2;
while(count<num)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
count++;
cout<<"\t"<<n3;
}
return 0;
}``````

## 4. Program Parts –

1. #include is a pre-processor directive. It is used to include header files.
2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. int is data type. It indicates that the program returns a value.
5. main() is main function that indicates the compiler that the user written programs starts from here.
6. num, n1, n2, n3, count are variable names.
7. print/cout is the keyword use to print a message.
8. scanf/cin is the keyword use to store values in variables.
9. return statement is use to return a value. If any error occurs the program will return 0.

## 4. Explanation –

Compiler checks the necessary header files in the program. After header files are found it advances further. Compiler enters main function. Here we declare five variables num, n1, n2, n3, count. All of integer data type. Then it asks user to put the limit for fibonacci series. It is stored in num. In next step we print first two numbers stored in n1 and n2. Then we enter while loop.

Here we evaluate next numbers of fibonacci series. We add previous two values and store addition in third variable n3. Then we store value of n2 in n1 and that of n3 in n1. In next step we print value of n3. And continue to do it so till value of count is less than num value. This is the main logic of fibonacci series i.e. the next value is the sum of previous two values. After the loop is executed the program execution terminates.

## Conclusion-

This was the program for Fibonacci series in C/C++. We hope it was clear to you. If you find it useful then do share it with your programming buddies and friends. Also if you have any doubt regarding any post do tell us in the comment section.

View All Posts

### 1 COMMENT ##### https://blog.parcian.com/best-content-management-systems/February 23, 2021 1:48 am

Hola I quite like reading through an article that will make men and women think. Also, thanks for allowing me to comment! thank u