27/03/2023

Write a Program to Print Fibonacci Series in C++ | Example

Program to Print Fibonacci Series in C++. As we know that Fibonacci Series is started with Zero (0) and the next Element is One Then we add the previous two elements and print the next element of the Fibonacci Series. This process repeats up to n times, suppose you want to print a Fibonacci Series in C++ for up to 5 terms then you need to repeat this process up to 5 times and so on.

Fibonacci Series start with a Zero and the next element is one then first we print 0 and 1. Now add two previous elements and print the next element as 0+1=1. repeat the process again and again until you get your answer.

0+1=1
1+1=2
1+2=3
2+3=5
3+5=8
5+8=13
8+13=21
13+21=34. . . .

Fibonacci Series in C++

Fibonacci Series in C++ 


#include <iostream>
using namespace std;
int main()
{
	int a = 0, b = 1, i, n, c;

	cout << "Enter The Number \n\n";	//Enter the number of terms
	cin >> n;

	cout << "\nFibonacci Series Is \n\n";	//Fibonacci Series Is given below
	cout << a << " " << b << " ";

	for (i = 0; i < n; i++)
	{
		c = a + b;	//here adding two previous value
		cout << c << " ";
		a = b;	//swap the value
		b = c;	//here first two value results are stored in 'b' }	//Loop end.
	cout << "\n";
	return 0;

}

Fibonacci Series


Fibonacci Series in C++: In mathematics, the Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterized by the fact that every number after the first two is the sum of the two preceding ones.

Fibonacci Series: 1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.

Often, especially in modern usage, the sequence is extended by one more initial term.

Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34.

The Fibonacci numbers occur in the sums of "shallow" diagonals in Pascal's triangle seen in the given picture

Important Fact about the Fibonacci Series: 'The 'Golden Ratio' or Phi' of any two consequent numbers of the sequence in the Fibonacci Series in C++ is 1.618 or 1.6 (approx) Except for starting 4 terms.

Example: The first 21 Fibonacci numbers Fn for n = 0, 1, 2, …, 20 are given below.

Fibonacci Series: 0 ,1 ,1 ,2 ,3 ,5 ,8 ,13 ,21 ,34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765.

3 / 2 = 1.500
5 / 3 = 1.667
8 / 5 = 1.600
13 / 8 = 1.625
21 / 13 = 1.615
34 / 21 = 1.619
55 / 34 = 1.617
89 / 55 = 1.618
144 / 89 = 1.618
233 / 144 = 1.618
377 / 233 = 1.618
610 / 377 = 1.618
987 / 610 = 1.618
1597 / 987 = 1.618
2584 / 1597 = 1.618
4181 / 2584 = 1.618
6765 / 4181 = 1.618

The Output of the Fibonacci Series


The Output of the Fibonacci Series

Similar to Fibonacci Series


Previous Post
Next Post

post written by:

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.
Follow Me

3 comments: