answersLogoWhite

0


Best Answer

void print_fib (unsigned terms) {

unsigned f1 = 0; // the first term

unsigned f2 = 1; // the second term

while (terms--) {

std::cout<<f1<<std::endl;

unsigned f3 = f1 + f2; // the next term

f1 = f2; // shift the terms for the next iteration

f2 = f3;

}

}

User Avatar

Wiki User

6y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is an algorithm to print the Fibonacci series 0123 up to n terms?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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)); }


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(); }


Write a Flowchart for Fibonacci series?

1.start 2.a=0,b=1,c and counter 3.display a 4.display b 5.c=a+b 6.display c 7.a=b 8.b=c 9.check whether number is less than the last number you have if yes than go to step 5 if no stop it


How do you write a C plus plus program to display the Fibonacci sequence to 10 terms and then display the 20th term?

#include&lt;iostream&gt; #include&lt;vector&gt; #include&lt;cassert&gt; using namespace std; // Returns a vector of Fibonacci numbers from start to max. // Sequence A000045 in OEIS if start is 0. vector&lt;unsigned&gt; fibonacci (const unsigned start, const unsigned max) { // Invariants: if (1&lt;start) throw std::range_error ("vector&lt;unsigned&gt; fibonacci (const unsigned start, const unsigned max): start &lt; 1"); if (max&lt;start) throw std::range_error ("vector&lt;unsigned&gt; fibonacci (const unsigned start, const unsigned max): max &lt; start"); // Empty set... vector&lt;unsigned&gt; fib {}; if (max) { // First term... fib.push_back (start); if (1&lt;max) { // Second term... fib.push_back (1); // All remaining terms... unsigned next = 0; while ((next = fib.back()+fib[fib.size()-2]) &lt;= max) fib.push_back (next); } } return fib; }; // Return true if the given number is prime. bool is_prime (const unsigned num) { if (num&lt;2) return false; if (!(num%2)) return num==2; for (unsigned div=3; div&lt;=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&lt;unsigned&gt; f = fibonacci (1, max); cout &lt;&lt; "Prime Fibonacci numbers in range [1:" &lt;&lt; max &lt;&lt; "]\n"; for (auto n : f) if (is_prime (n)) cout &lt;&lt; n &lt;&lt; ", "; cout &lt;&lt; "\b\b " &lt;&lt; endl; // backspace and overwrite trailing comma }


How do you write an algorithm for fibonic number?

In a Fibonacci sequence, the previous two numbers are added to generate the next Fibonacci number. F1=1st number F2=2nd number F3=f1+f2=1+2=3 F4=f2+f3=2+3=5 F5=f3+f4=3+5=8, and so on. f1 f2 f3 f4 f5 f6 f7.............. 1 2 3 5 8 13 21............. In algorithm: 1. Assign sum=0, A=0, B=1, i=1 2. Get the no. of terms upto which u want to generate the Fibonacci no, i.e., n. 3.Add A and B to get the next Fibonacci number 4. Assign the value of B to A i.e. A=B 5. Assign the value of sum to B i.e. B=sum 6. Write the value of su to get next Fibonacci number in the series. 7. increment i with 1 i.e. i=i+1 and repeat step 3,4,5,6 with the last value of i=n(n is the no. of terms which u wnt to generate Fibonacci no. series.) 8. Stop

Related questions

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 relationship between the golden ratio and the standard Fibonacci sequence?

The "golden ratio" is the limit of the ratio between consecutive terms of the Fibonacci series. That means that when you take two consecutive terms out of your Fibonacci series and divide them, the quotient is near the golden ratio, and the longer the piece of the Fibonacci series is that you use, the nearer the quotient is. The Fibonacci series has the property that it converges quickly, so even if you only look at the quotient of, say, the 9th and 10th terms, you're already going to be darn close. The exact value of the golden ratio is [1 + sqrt(5)]/2


What is the pattern in Fibonacci numbers?

Each term of the series is the sum of the two terms before it.


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)); }


What is the name of the sequence 123581321?

With a few added commas, hyphens, or spaces, those could be the first 7 terms of the Fibonacci Series.


How is the golden ratio devised?

The 'golden ratio' is the limit of the ratio of two consecutive terms of the Fibonacci series, as the series becomes very long. Actually, the series converges very quickly ... after only 10 terms, the ratio of consecutive terms is already within 0.03% of the golden ratio.


Can you have the program in C plus plus for recursive function generating a series of terms?

Yes, this can be done. For example for Fibonacci series. You will find plenty of examples if you google for the types of series you need to be generated.


What is the next in this sequence 112358?

There are many possible answers. One obvious one is 13, the next number in the Fibonacci Sequence that yields the golden mean.


What is the rule for 1 1 2 3 5?

Each term is the sum of the two terms before it. That's the famous "Fibonacci" series.


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

This is called a Fibonacci series after the Italian mathematician who described it.


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 pattern to 5 8 13 21?

Part of the Fibonacci Series, where each term is the sum of the previous 2 terms. Named after Leonard of Pisa.