In: Computer Science
A Fibonacci sequence, is a sequence of numbers with the property that each number is the sum of the two preceding Fibonacci numbers, starting from 0 and 1. Fibonacci number are usually denoted by Fn, where Fn is the nth Fibonacci number. Thus Fn = 0, and Fn = 1, and Fn = Fn-1 + Fn-2, n ≥ 2. Here are the first few Fibonacci numbers: F0=0 (by definition) F1=1 (by definition) F2 = F1 + F0 = 1 + 0 = 1 F3 = F2 + F1 = 1 + 1 = 2 F4 = F3 + F2 = 2 + 1 = 3 F5 = F4 + F3 = 3 + 2 = 5 F6 = F5 + F4 = 5 + 3 = 8 F7 = F6 + F5 = 8 + 5 = 13 F8 = F7+ F6 = 13 + 8 = 21 Write a C or C++ program that computes and prints F0 through F8 , one number per line, using a loop. For initialization purposes, you may assume F0 = 0, and F1 = 1. Write a C or C++ program that computes and prints F8 through F2 (i.e., in reverse order), one number per line, using a loop. For initialization purposes, you may assume F8 = 21, and F7 = 13.
Program 1
############
//####################################### PGM START ####################
#include<iostream>
using namespace std;
int main(){
int F0=0;
int F1=1;
int F;
//print fo anf f1
cout<<"F0 = "<<F0<<"\n";
cout<<"F1 = "<<F1<<"\n";
//print from f2 to f8
for(int i=0;i<=6;i++){
F=F0+F1;
F0=F1;
F1=F;
cout<<"F"<<(i+2)<<" =
"<<F<<"\n";
}
return 0;
}
//################################### PGM END ##############################
OUTPUT
##########
2) Program 2
###########
//############################### PGM START ##################################
#include<iostream>
using namespace std;
int main(){
int F8=21;
int F7=13;
int F;
//print f8 anf f7
cout<<"F8 = "<<F8<<"\n";
cout<<"F7 = "<<F7<<"\n";
//print from f6 to f2
for(int i=6;i>=2;i--){
F=F8-F7;
F8=F7;
F7=F;
cout<<"F"<<i<<" =
"<<F<<"\n";
}
return 0;
}
//############################### PGM END #####################################
OUTPUT
#########