Solve the problem in C++ The terms of the Fibonnaci Sequence are the numbers 1,
ID: 3804362 • Letter: S
Question
Solve the problem in C++
The terms of the Fibonnaci Sequence are the numbers 1, 1, 2, 3, 5, 8, 13, --------------. The next term in the sequence is found by summing the previous two terms.
Numbers in an ACSLacci sequence are formed by the previous three terms and subtracting 3. For Example the ACSLacci Sequence starting with 1, 0 and 4 is as follows.
1, 0, 4, 2, 3, 6, 8, 14 25, ---,
Input.
Five set of data. Each set consist of four integers. a1, a2, a3 and N. The first three numbers are the first three terms of an ACSLacci sequence . The fourth number is the term to print.
output:
For each input set, print the Nth term of the ACSLacci sequence defined by the first three numbers in the input set.
Sample input.
Line #1: 1, 0, 4, 9
Line #2: 1, 1, 2, 5
Sample Out
output #1: 25
output #2: 1
Explanation / Answer
#include <iostream>
#include <string>
int NthACSLacci(int a1,int a2,int a3,int N);
int main()
{
int a1,a2,a3,an,N;
std::cout << "Enter values (a1 a2 a3 N): ";
std::cin>>a1>>a2>>a3>>N;
std::cout << "input: " << a1 << ","<< a2<<","<<a3<<","<<N<<" ";
an = NthACSLacci(a1,a2,a3,N);
std::cout << "output: " <<an<<" ";
return 0;
}
int NthACSLacci(int a1,int a2,int a3,int N){
int count = 3;
int an = 0;
while(count<N){
an = a1+a2+a3-3;
count++;
a1=a2;
a2=a3;
a3=an;
}
return an;
}
#include <iostream>
#include <string>
int NthACSLacci(int a1,int a2,int a3,int N);
int main()
{
int a1,a2,a3,an,N;
std::cout << "Enter values (a1 a2 a3 N): ";
std::cin>>a1>>a2>>a3>>N;
std::cout << "input: " << a1 << ","<< a2<<","<<a3<<","<<N<<" ";
an = NthACSLacci(a1,a2,a3,N);
std::cout << "output: " <<an<<" ";
return 0;
}
int NthACSLacci(int a1,int a2,int a3,int N){
int count = 3;
int an = 0;
while(count<N){
an = a1+a2+a3-3;
count++;
a1=a2;
a2=a3;
a3=an;
}
return an;
}
#include <iostream>
#include <string>
int NthACSLacci(int a1,int a2,int a3,int N);
int main()
{
int a1,a2,a3,an,N;
std::cout << "Enter values (a1 a2 a3 N): ";
std::cin>>a1>>a2>>a3>>N;
std::cout << "input: " << a1 << ","<< a2<<","<<a3<<","<<N<<" ";
an = NthACSLacci(a1,a2,a3,N);
std::cout << "output: " <<an<<" ";
return 0;
}
int NthACSLacci(int a1,int a2,int a3,int N){
int count = 3;
int an = 0;
while(count<N){
an = a1+a2+a3-3;
count++;
a1=a2;
a2=a3;
a3=an;
}
return an;
}
=======================OUTPUT================
Enter values (a1 a2 a3 N): 1 0 4 9
input: 1,0,4,9
output: 25
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.