Fibonacci numbers in a star pattern

Write a C++ program to write Fibonacci numbers in the. You should input the number of lines of Fibonacci numbers you want in your program.

The Fibonacci sequence is given by

$$F_i = \begin{cases} F_0 = 0, \\ F_1 = 1, \\ F_n = F_{n-1}+F_{n-2} \end{cases} $$

We need Fibonacci in star pattern as shown below

    1 
   1 2 
  3 5 8 
 13 21 34 55 
89 144 233 377 610 

Please try this on your own first. The code is specified below

#include<iostream>
using namespace std;
int main(){
	int f0=0,f1=1,f2=1;
	int n=5;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if((i+j)<(n-1))cout<<" ";
			else {
			    cout<<f2<<" ";
			    f2 = f1 + f0;
		        f0 = f1;
		        f1 = f2;
			}
		}
		cout<<endl;
	}
	return 0;
}

The output of the program is

output of the above program

Leave a Reply