answersLogoWhite

0


Best Answer

#include<iostream>

#include<vector>

#include<cassert>

using namespace std;

// Returns a vector of Fibonacci numbers from start to max.

// Sequence A000045 in OEIS if start is 0.

vector<unsigned> fibonacci (const unsigned start, const unsigned max)

{

// Invariants:

if (1<start)

throw std::range_error

("vector<unsigned> fibonacci (const unsigned start, const unsigned max): start < 1");

if (max<start)

throw std::range_error

("vector<unsigned> fibonacci (const unsigned start, const unsigned max): max < start");

// Empty set...

vector<unsigned> fib {};

if (max)

{

// First term...

fib.push_back (start);

if (1<max)

{

// Second term...

fib.push_back (1);

// All remaining terms...

unsigned next = 0;

while ((next = fib.back()+fib[fib.size()-2]) <= max)

fib.push_back (next);

}

}

return fib;

};

// Return true if the given number is prime.

bool is_prime (const unsigned num)

{

if (num<2) return false;

if (!(num%2)) return num==2;

for (unsigned div=3; div<=sqrt(num); div+=2)

if (!(num%div)) return false;

return true;

}

// Displays all prime Fibonacci numbers in range [1:10,000].

int main()

{

const unsigned max=10000;

vector<unsigned> f = fibonacci (1, max);

cout << "Prime Fibonacci numbers in range [1:" << max << "]\n";

for (auto n : f)

if (is_prime (n))

cout << n << ", ";

cout << "\b\b " << endl; // backspace and overwrite trailing comma

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

9y ago

#include<iostream>

#include<vector>

#include<cassert>

using namespace std;

// Returns a vector of Fibonacci numbers from 0 to the nth term.

// Sequence A000045 in OEIS.

vector<unsigned> fibonacci (const unsigned terms)

{

// Empty set...

vector<unsigned> fib {};

if (terms)

{

// First term...

fib.push_back (0);

if (1<terms)

{

// Second term...

fib.push_back (1);

// All remaining terms (2 to n-1).

for (unsigned term=2; term<terms; ++term)

fib.push_back (fib[term-2]+fib[term-1]);

}

}

return fib;

};

// Displays Fibonacci sequence to 10 terms followed by the 20th term.

int main()

{

const unsigned max=20;

vector<unsigned> f = fibonacci (max);

assert (f.size()==max);

cout << "Fibonacci Sequence\n10 terms: ";

for (unsigned n=0; n<10; ++n)

cout << f[n] << ", ";

cout << "\b\b "; // backspace and overwrite trailing comma

cout << "\n20th term: " << f[max-1] << std::endl;

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

You can use a loop statement to input the numbers. Here is a code snippet:

int i, arr[10];

cout<<"Enter the 10 numbers: ";

for( i=0; i<10; i++)

cin>>arr[i];//Inserting the elements in the array

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you write a C plus plus program to display the Fibonacci sequence to 10 terms and then display the 20th term?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you check whether a given number n is a Fibonacci number or not?

In a Fibonacci sequence, sum of two successive terms gives the third term.... here is the Fibonacci sequence: 0,1,1,2,3,5,8,13,21,34,55,89,144........ General formula to generate a Fibonacci sequence is """Fn= Fn-1 + Fn-2""" To check whether a number is Fibonacci or not follow the following steps: 1) Get the number as input from user. 2) Fix the first two numbers of sequence as 0 and 1. 3) put a sentinel loop with upper limit being the input number. 4)in the body of loop generate the next number in sequence in each iteration and continue swapping the values as follows: a=0 b=1 next=a+b while (next&lt; input) a=b b=next next=a+b wend 5) lastly when program exits the loop compare the last number of sequence with the input number if they are equal then number is Fibonacci otherwise not. otherwise the last term of sequence will be less than the input number.


What is a recursive rule?

It is a term for sequences in which a finite number of terms are defined explicitly and then all subsequent terms are defined by the preceding terms. The best known example is probably the Fibonacci sequence in which the first two terms are defined explicitly and after that the definition is recursive: x1 = 1 x2 = 1 xn = xn-1 + xn-2 for n = 3, 4, ...


Write a program to generate the Fibonacci Series using array?

#include&lt;stdlib.h&gt; #include&lt;conio.h&gt; #include&lt;stdio.h&gt; void main (void) { clrscr(); int i; int a[10]; a[0]=0; a[1]=1; printf("First 10 Fibonacci numbers are :\n"); printf("%d\n%d\n",a[0],a[1]); for(i=2;i&lt;10;i++) { a[i]=a[i-1]+a[i-2]; printf("%d\n",a[i]); } getch(); }


Write a program to genarate Fibonacci series upto sum numbers?

void main() { int n,old=0,curr=1,new=0; clrscr(); printf("enter the total number of terms up to which you want to print the Fibonacci series"); scanf("%d",&amp;n); printf("%d",old); printf("\n%d",curr); for(i=1;i&lt;=n;i++) { new=old+curr; old=curr; curr=new; printf("\n%d",new); } getch(); }


Algorithm of Fibonacci series in c?

#include&lt;stdio.h&gt; #include&lt;conio.h&gt; int fib(int a); main() { int a; clrscr(); scanf("%d",&amp;a); for(int i=0;i&lt;a;i++) printf("%d\n",fib(i)); } int fib(int a) { if(a==0) return 0; if(a==1) return 1; else return (fib(a-1)+fib(a-2)); }

Related questions

Is The Fibonacci sequence arithmetic?

No, the Fibonacci sequence is not an arithmetic because the difference between consecutive terms is not constant


List the first eight terms of the sequence formed by finding the differences of successive terms in the Fibonacci sequence?

0,1,1,2,3,5,8,13


Explain how to find the terms of Fibonacci sequence?

If the Fibonacci sequence is denoted by F(n), where n is the first term in the sequence then the following equation obtains for n = 0.


What is the sequence of number which the next term is formed by adding the last two terms?

Fibonacci sequence


Is the Fibonacci sequence a geometric sequence?

No. Although the ratios of the terms in the Fibonacci sequence do approach a constant, phi, in order for the Fibonacci sequence to be a geometric sequence the ratio of ALL of the terms has to be a constant, not just approaching one. A simple counterexample to show that this is not true is to notice that 1/1 is not equal to 2/1, nor is 3/2, 5/3, 8/5...


What are the relations between the golden ratio and the Fibonacci series?

The ratio of successive terms in the Fibonacci sequence approaches the Golden ratio as the number of terms increases.


What is the sequence of number in which the next term is formed by adding the last two terms called?

Fibonacci sequence


Find the next 2 terms of the Fibonacci sequence 2.2.4.6?

They are: 10 and 16


What are the next three terms in the Fibonacci sequence 1123581321?

34-55-89 are.


Is 112233 a Fibonacci sequence?

NO, its not a Fibonacci Sequence, but it is very close. The Fibonacci Sequence is a series of numbers in which one term is the sum of the previous two terms. The Fibonacci Sequence would go as follows: 0,1,1,2,3,5,8,13,21,..... So 0+1=1, 1+1=2, 1+2=3, 2+3=5, ans so on.


What is the ratio of the sequence?

The answer depends on the sequence. The ratio of terms in the Fibonacci sequence, for example, tends to 0.5*(1+sqrt(5)), which is phi, the Golden ratio.


Can you start with any two numbers when using finonacci's sequence?

The Fibonacci sequence starts with 1 and 1. However any sequence in which the first two terms are given and the rest are defined recursively as t(n) = t(n-2) + t(n-1), with n = 3, 4, ... is also known as a Fibonacci sequence. Note the "the" and "a" preceding Fibonacci sequence.