answersLogoWhite

0


Best Answer

#include<stdio.h>

#include<conio.h>

int fib(int);

int f=1,j=0,k=0,i=0,c;

void main()

{

int l;

clrscr();

printf("Enter limit\n");

scanf("%d",&l);

f=0;

printf("%d\n",f);

f=1;

printf("%d\n",f);

for(c=0;c<l;c++)

{

f=fib(l);

printf("%d\n",f);

}

getch();

}

int fib(int n)

{

while(i<n)

{

if(i<=n)

{

i++;

j=k;

k=f;

f=k+j;

fib(1);

return f;

}

}

}

output-

enter limit

4

0

1

1

2

3

5

User Avatar

Wiki User

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

Wiki User

13y ago

We need to make a method, we'll call it f(). We know that f(1), which is the first Fibonacci number, equals 1, and f(2), the second, also equals 1. f(3) = 2, f(4) = 3, f(5) = 5 and so on.

We compute these values by adding the two previous numbers, which means that f(n) equals f(n-1) plus f(n-2).

public double f(int n) {

if (n 2)

return 1.0;

else

return f(n-1) + f(n-2);

}

This method will do exactly that, it will invoke the two previous versions of itself, and those invoked will invoke their previous selves and so on until one of them reaches f(2) or f(1).

It's easy to use this method in a loop to print out many Fibonacci numbers:

for (i=1; i<20; i++)

System.out.println(f(i));

This will print out the first 20 Fibonacci numbers, but anything above 40 will probably be very slow because it has to recursively compute every number, larger numbers could take whole days to compute.

To get around this slowness you can store the value of each computed Fibonacci number into an array, every time f() is used it will check the array to see if the two previous numbers are already computed and saved to the array rather than starting from scratch each time. By doing this, you can compute over a 1000 numbers in less than 1 second, the highest possible being f(1476) because anything higher will cause the double value to overflow.

In fact, you dont' even need to use a double for the Fibonacci sequence, an int datatype will work fine, since the sequence starts with an integer, it cannot possible generate a double or float. In fact, using a double will lower the number of values the program can generate before it overflows as the double datatype uses more memory than an int datatype

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

int fibonacci(int n);

void main()

{

int n,i=0,c;

clrscr();

printf("enter the no. upto which you want the fibonacci series:\n");

scanf("%d",&n);

printf("FIBONACCI SERIES:\n");

for(c=1;c<n;c++)

{

printf("%d\n",fibonacci(i));

i++;

}

getch();

}

int fibonacci(int n)

{

if(n==0)

return 0;

else if (n==1)

return 1;

else

return(fibonacci(n-1)+fibonacci(n-2));

}

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

Possible, but inefficient.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Program demonstrate Fibonacci series using recursion?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Fibbomacci series using recursion shell programming?

Here is a good answer for recursion Fibonacci series. #include &lt;stdio.h&gt; #include &lt;conio.h&gt; long Fibonacci(long n); int main() { long r, n,i; printf("Enter the value of n: "); scanf("%ld",&amp;n); for(i=0;i&lt;=n;i++) { printf(" Fibonacci(%ld)= %ld\n", i,Fibonacci(i)); } getch(); return 0; } long Fibonacci(long n) { if(n==0 n==1) return n; else { return (Fibonacci(n-1)+Fibonacci(n-2)); } } for n=5; Output: Fibonacci(0)=0 Fibonacci(1)=1 Fibonacci(2)=1 Fibonacci(3)=2 Fibonacci(4)=3 Fibonacci(5)=5


How do you write a program to print Fibonacci series of n numbers using recursion in cpp?

#include&lt;iostream&gt; unsigned fib (unsigned term, unsigned a=0, unsigned b=1) { if (term&lt;1) return a; return fib (--term, a+b, a); } int main() { std::cout &lt;&lt; "Fibonacci (1000th term): " &lt;&lt; fib (1000) &lt;&lt; std::endl; }


Can you explain the program of Fibonacci series in c?

You mean you have written a program, but you don't understand it? Well, how could I explain it without seeing it?


Write a program to print the Fibonacci series in php upto input value using recursive function?

The Fibonacci sequence uses recursion to derive answers. It is defined as: F0 = 0 F1 = 1 Fn = F(n - 1) + F(n -2) To have this sequence printed by a php script use the following: function fibonacci($n) { if($n 1) return 1; //F1 else return fibonacci($n - 1) + fibonacci($n - 2); //Fn } This recursive function will print out the Fibonacci number for the integer n. To make it print out all the numbers in a particular set add this to your script. for($i = 0; $i &lt; 15; $i++) { echo fibonacci($i) . "&lt;br /&gt;"; } So your final result would look like. &lt;?php function fibonacci($n) { if($n 1) return 1; else return fibonacci($n - 1) + fibonacci($n - 2); } for($i = 0; $i &lt; 15; $i++) { echo fibonacci($i) . "&lt;br /&gt;"; } ?&gt;


Derive recursion formula for sin by using Taylor's Series?

the Taylor series of sinx

Related questions

Fibbomacci series using recursion shell programming?

Here is a good answer for recursion Fibonacci series. #include &lt;stdio.h&gt; #include &lt;conio.h&gt; long Fibonacci(long n); int main() { long r, n,i; printf("Enter the value of n: "); scanf("%ld",&amp;n); for(i=0;i&lt;=n;i++) { printf(" Fibonacci(%ld)= %ld\n", i,Fibonacci(i)); } getch(); return 0; } long Fibonacci(long n) { if(n==0 n==1) return n; else { return (Fibonacci(n-1)+Fibonacci(n-2)); } } for n=5; Output: Fibonacci(0)=0 Fibonacci(1)=1 Fibonacci(2)=1 Fibonacci(3)=2 Fibonacci(4)=3 Fibonacci(5)=5


What is recursion why do you need it?

Recursion is a process by which a method calls itself over again until some process is complete or some condition is met. we need recursion for solving those problem whose end is infinite like in case of Fibonacci series generation etc.


How do you write a program to print Fibonacci series of n numbers using recursion in cpp?

#include&lt;iostream&gt; unsigned fib (unsigned term, unsigned a=0, unsigned b=1) { if (term&lt;1) return a; return fib (--term, a+b, a); } int main() { std::cout &lt;&lt; "Fibonacci (1000th term): " &lt;&lt; fib (1000) &lt;&lt; std::endl; }


Write a c program Fibonacci series using for loop in java?

Exactly what do you mean by 'C program in Java'


What are the merits and demerits of recursion?

Ans: Merits of recursion are: Mathematical functions, such as Fibonacci series generation can be easily implemented using recursion as compared to iteration technique. Demerits of recursion are: Many programming languages do not support recursion; hence, recursive mathematical function is implemented using iterative methods. Even though mathematical functions can be easily implemented using recursion, it is always at the cost of execution time and memory space. The recursive programs take considerably more storage and take more time during processing.


Can you explain the program of Fibonacci series in c?

You mean you have written a program, but you don't understand it? Well, how could I explain it without seeing it?


Program to generate Fibonacci series using storage class in c plus plus?

i dn't know. haha


Write a program to print the Fibonacci series in php upto input value using recursive function?

The Fibonacci sequence uses recursion to derive answers. It is defined as: F0 = 0 F1 = 1 Fn = F(n - 1) + F(n -2) To have this sequence printed by a php script use the following: function fibonacci($n) { if($n 1) return 1; //F1 else return fibonacci($n - 1) + fibonacci($n - 2); //Fn } This recursive function will print out the Fibonacci number for the integer n. To make it print out all the numbers in a particular set add this to your script. for($i = 0; $i &lt; 15; $i++) { echo fibonacci($i) . "&lt;br /&gt;"; } So your final result would look like. &lt;?php function fibonacci($n) { if($n 1) return 1; else return fibonacci($n - 1) + fibonacci($n - 2); } for($i = 0; $i &lt; 15; $i++) { echo fibonacci($i) . "&lt;br /&gt;"; } ?&gt;


Is 20 a Fibonacci number?

20 is not a term in the Fibonacci series.


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.


Derive recursion formula for sin by using Taylor's Series?

the Taylor series of sinx


How many types of recursion are there in c language?

Recursion in c language is a method where the function calls itself, within or outside the scope. Using Recursion, complicated problems can be divided into smaller parts so that solving them becomes more manageable. The recursion technique is available in Java, JavaScript, and C++.serves the same purpose. The type of Recursion in C • Direct Recursion • Indirect Recursion. Direct Recursion Recursion can call the function n-number of times. In the case of direct Recursion, the function calls itself inside the same position or in the local scope Direct Recursion problems are the Fibonacci series, a program to print 50 natural numbers. Indirect Recursion In the case of Indirect Recursion, a function X calls function Y, and function Y calls any function Z. Under certain conditions, function Z calls function A. In this case, function A is indirectly related to function Z. Indirect Recursion is also known as mutual Recursion, as more than one function runs a program. It is a two-step recursive function call process for making a recursive function call. Below mentioned are also type of Recursion: Tail Recursion No Tail/Head Recursion Linear Recursion Tree Recursion Tail Recursion A function is said to be tail recursion if it calls itself and also calls the last or the previous statement executed in the process. Head Recursion A function is said to be Head Recursion if it calls itself and also calls the first or the beginning statement executed in the process. Linear Recursion A function is said to be a linear recursive function if it makes a single call to itself each time the procedure executes itself and grows linearly depending on the size of the problem. Tree Recursion Tree Recursion is different from linear Recursion. Rather than making only one call to itself, that function makes more than one recursive call to the process within the recursive function. Following are the steps to solve the recursive problem in C: Step 1: Create a function and assign the work a part should do. Step 2: Select the subproblem and assume that the function already works on the problem. Step 3: Get the answer to the subproblem and use it to resolve the main issue. Step 4: The 90% of the problem defined is solved.