The sequence 11235813 is called Fibonaccis sequence and itis

The sequence

1,1,2,3,5,8,13, ...is called Fibonacci’s sequence and itis defined recursively as t1= 1, t2= 1, tn=tn1+tn2

for nN and n >2.

Let Fibonacci’s sequence mod 10 be defined as m1= 1, m2= 1 and mn= (mn1+mn2) % 10

for nN and n >2. Write a C ++ program that verifies the following facts about

Fibonacci’s sequence mod 10 for the number of terms requested by the user:

•Every third term is even.

•Every fifth term is divisible by 5.

•Every fifteenth term is 0.

Assume that the program will prompt the user to enter the number of terms repeatedly

until the user wants to quit.

Use a do...while loop to implement repeating until the user wants to quit.

Use a for loop to output the number of terms requested.

Solution

#include <iostream>
using namespace std;

void getFibonacciSequence(int *arr,int number)
{
int x = 1,y=1,z,i;
  
if(number==0)
return;
else if(number==1)
{
arr[0] = 1;
return;
}
else if(number==2)
{
arr[0] = arr[1] = 1;
return;
}
else
{
arr[0] = arr[1] = 1;
}
  
for(i=2;i<number;i++)
{
arr[i] = (x+y)%10;
x = y;
y = arr[i];
}
}

bool every3rdIsEven(int *arr,int number)
{
bool result = true;
int i;
for(i=0;i<number;i++)
{
if((i+1)%3==0)
{
if(arr[i]%2!=0)
{
result = false;
break;
}
}
  
}
  
return result;
}


bool every5thDivisibleBy5(int *arr,int number)
{
bool result = true;
int i;
for(i=0;i<number;i++)
{
if((i+1)%5==0)
{
if(arr[i]%5!=0)
{
result = false;
break;
}
}
  
}
  
return result;
}

bool every15thElementIsZero(int *arr,int number)
{
bool result = true;
int i;
for(i=0;i<number;i++)
{
if((i+1)%15==0)
{
if(arr[i]!=0)
{
result = false;
break;
}
}
  
}
  
return result;
}

void printFibonacciSequence(int *arr,int number)
{
int i=0;
for(i=0;i<number;i++)
cout << arr[i] << \" \";
cout << endl;
}

int main() {
   // your code goes here
   char choice;
  
   do
   {
   int *arr;
   int number;
   cout << \"Enter the number of fibonacci terms you want : \ \";
   cin >> number;
  
   arr = new int[number];
   getFibonacciSequence(arr,number);
   printFibonacciSequence(arr,number);
  
   if(every3rdIsEven(arr,number)==true)
   cout << \"Every third element is Even.\" << endl;
   else
   cout << \"Every third element is not Even.\" << endl;
  
   if(every5thDivisibleBy5(arr,number)==true)
   cout << \"Every fifth element is divisible by 5.\" << endl;
   else
   cout << \"Every fifth element is not divisible by 5.\" << endl;
  
   if(every15thElementIsZero(arr,number)==true)
   cout << \"Every fifteenth element is zero.\" << endl;
   else
   cout << \"Every fifteenth element is not zero.\" << endl;
  
   cout << \"Do you want to quit?(q)\";
   cin >> choice;
   }while(choice!=\'q\');
   return 0;
}

OUTPUT:

Enter the number of fibonacci terms you want :

20
1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5
Every third element is Even.
Every fifth element is divisible by 5.
Every fifteenth element is zero.
Do you want to quit?(q) n

Enter the number of fibonacci terms you want :

30
1 1 2 3 5 8 3 1 4 5 9 4 3 7 0 7 7 4 1 5 6 1 7 8 5 3 8 1 9 0
Every third element is Even.
Every fifth element is divisible by 5.
Every fifteenth element is zero.
Do you want to quit?(q) q

The sequence 1,1,2,3,5,8,13, ...is called Fibonacci’s sequence and itis defined recursively as t1= 1, t2= 1, tn=tn1+tn2 for nN and n >2. Let Fibonacci’s sequ
The sequence 1,1,2,3,5,8,13, ...is called Fibonacci’s sequence and itis defined recursively as t1= 1, t2= 1, tn=tn1+tn2 for nN and n >2. Let Fibonacci’s sequ
The sequence 1,1,2,3,5,8,13, ...is called Fibonacci’s sequence and itis defined recursively as t1= 1, t2= 1, tn=tn1+tn2 for nN and n >2. Let Fibonacci’s sequ

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site