hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.
Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.
and than you
22 122479 Banfa 9,065
Expert Mod 8TB
This is very simply achieved with a loop, you should have a go yourself and then we will help if there are problems in your solution.
There is a formula as well which you should be able to find with a few minutes web search. However it uses the floating point calculations on an irrational number so may not be entirely suited to a computer (where floating point calculations are not exact).
There is a simple recurrence relation which will help.
fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). That's the recurrence relation which is recursive, but it is easily implemented as a loop.
//Non recursive method -
#include<stdio.h>
-
#include<conio.h>
-
-
void main()
-
{
-
int initial_value=0,final_value=1,temp_value,count=10;
-
// count contains the number of elements to be generated.....
-
for(count=1;count<=10;count++)
-
{
-
temp_value=initial_value+final_value;
-
printf("\n%d",initial_value);
-
initial_value=final_value;
-
final_value=temp_value;
-
}
-
-
getch();
-
}
- // Recursive Method.......
-
-
int fibo_gen(int,int,int);
-
-
void main()
-
-
{
-
-
int initial_value=0,final_value=1,count=100;
-
fibo_gen(initial_value,final_value,count);
-
getch();
-
-
}
-
-
int fibo_gen(int initial_value,int final_value,int count)
-
-
{
-
-
int temp;
-
-
if (count>0)
-
-
{
-
-
printf("\n%d",initial_value);
-
temp=initial_value+final_value;
-
initial_value=final_value;
-
final_value=temp;
-
count=count-1;
-
fibo_gen(a,b,c);
-
-
}
-
-
else
-
return(0);
-
-
}
]
- main()
-
{
-
int n,fib[20];
-
printf("enter the value of n");
-
scanf("%d",&n);fib[0]=0;fib[1]=1;
-
for(i=2;i<n;i++)
-
fib(i)=fib(i-1)+fib(i-2);
Banfa 9,065
Expert Mod 8TB
main()
{
int n,fib[20];
printf("enter the value of n");
scanf("%d",&n);fib[0]=0;fib[1]=1;
for(i=2;i<n;i++)
fib(i)=fib(i-1)+fib(i-2);
You haven't output the result,
you haven't finished the program, this wont compile
if the user inputs a value >= 20 then this program writes off the end of the fib array invoking undefined behaviour.
Apart from that a very good solution :D
G'day
where error?
1 error(s), 0 warning(s) - # include <iostream.h>
-
int fibonacci(int n)
-
{
-
int x1 = 0, fib;
-
int x2 = 1;
-
if(n >= 1)
-
{
-
for(int i=2;i<= n; i++)
-
{
-
fib = x1+ x2;
-
x1 = x2;
-
x2 = fib;
-
}
-
-
return fib;
-
}
You only return a value if n > 0. Afterwards, you should add "return 1; }" assuming n is non-negative. Also, usually the sequence starts with 1 and 1, not 0 and 1. You may be off by one term.
#include<iostream>
using namespace std; -
int fibonacci(int n)
-
{
-
int x1 = 0, fib;
-
int x2 = 1;
-
if(n >= 1)
-
{
-
for(int i=2;i<= n; i++)
-
{
-
fib = x1+ x2;
-
x1 = x2;
-
x2 = fib;
-
}
-
}
-
-
return fib;
-
}
-
int main(){
-
for(int i=2;i<=10;i++) {
-
cout<<fibonacci(i)<<endl;
-
}
-
return 0;
-
}
this is ok now I'm finish it
but I need this
(1) Determine the largest int Fibonacci number that can be printed on your system.
(2) Modify the program of part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified program to repeat part (b).
The way integers are ordered, are non-negative numbers, in order, followed by the reversed negative range. It goes from 0 to INT_MAX, then INT_MIN to -1. Integers are 32-bit, so in Hexadecimal:
0x00000000 = 0
0x7FFFFFF = INT_MAX = -1+(2^31)
0x80000000 = INT_MIN = -(2^31)
0xFFFFFFF = -1.
INT_MAX + 1 = INT_MIN. Since Fibonacci numbers are positive, it will overflow and become negative. Modify the for statement to a while statement, and run the loop while fib is not negative (no overflow). Then variables x1 and x2, x1 < x2, will be the two most positive Fibonacci numbers an integer can support.
Hi,
Please follow the following program, - #include<stdio.h>
-
#include<conio.h>
-
void main()
-
{
-
int a =0,b=1,c,i=2,n;
-
clrscr();
-
printf("Enter N Value : ");
-
scanf("%d",&n);
-
printf("%d\t%d\t",a,b);
-
while(n>i)
-
{
-
c = a+b;
-
printf("%d\t",c);
-
c=b;
-
b=a;
-
i++;
-
}
-
getch();
-
}
Take Care,
Bye... Bye...
The easiset non recursive method I see to do this is to use a stack.
stack will always have 2 elements , both will have integers, and of course pointers.
The next element will be figured by adding the 2 integers in the stack. then you will push that on the top of the stack, and pull the header off of the stack.
I hope that made sence.
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.
Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.
and than you
hello friend,
one thing is remember whenever u r doing fib series most important part is fib generate recursion that is chain is occur. so make one function is fib(). around this function write the code just like this as follows
fo=0;
f1=1
fib=fo+f1;
and display the fib.
try your self otherwise i will send u full program for fib.
thank u very much.
Instead of a stack with two entries, just use two variables. Besides, there are already plenty of solutions posted here, and the iterative loop is considerably faster than using recursion.
Banfa 9,065
Expert Mod 8TB
i want it in c++ not c
And what do you think the difference should be?
Do you want to get hints and try yourself?
or do you want to get a readymade code,just as you buy things from shops?
i want it in c++ not c
- // Recursive method ...Try it.
-
-
#include <iostream.h>
-
-
static int fibVal = 0;
-
static int f0 = 0, f1 = 1;
-
-
int fib(int n)
-
{
-
if(n>0)
-
{
-
fibVal = f0 + f1;
-
f0 = f1;
-
f1 = fibVal;
-
cout<<f1<<endl;
-
n--;
-
fib(n);
-
}
-
return f1;
-
}
-
-
-
-
void main()
-
{
-
int limit;
-
cout<<"Please Enter the value = ";
-
cin>>limit;
-
int answer = 0;
-
-
if(limit == 1)
-
{
-
cout<<f0<<endl;
-
}
-
else if(limit > 1)
-
{
-
cout<<f0<<endl;
-
cout<<f1<<endl;
-
answer = fib(limit - 2);
-
}
-
else if(limit <= 0)
-
cout<<"Please enter the value greater than 0"<<endl;
-
-
}
hi
I need help with this problem. I have tried writting the program, i just can not get it to run properly.
Please Write
The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
(a) Write a nonrecursive function fibonacci( n ) that calculates the nth Fibonacci number.
and than you
plese give me the program.
plese give me the program.
Sure,just follow this link.
Savage
Hi,
Recursion in this case might cause the stack overflow for larger series. I think the most optimized piece of code can be
------------------------------------------------------------------------- - #include < iostream.h >
-
-
void main()
-
{
-
unsigned int initial_value = 0, final_value = 1, count = 0;
-
-
cout << "Give the count:";
-
cin >> count;
-
-
while( count-- ) {
-
cout << initial_value << endl;
-
final_value += initial_value;
-
initial_value = final_value - initial_value;
-
}
-
}
-------------------------------------------------------------------------
It prints till count 48. Perhaps you need larger datatype insted of 'int' in case you need larger series.
Enjoy!!!
Shashikant Patil
Hi,
Recursion in this case might cause the stack overflow for larger series. I think the most optimized piece of code can be -
Have a look at a closed form formula; it's equation (6) in this link.
kind regards,
Jos
This discussion thread is closed Replies have been disabled for this discussion. Similar topics
4 posts
views
Thread by YS Sze |
last post: by
|
12 posts
views
Thread by CII |
last post: by
| | | | | | | | | | | | | | | |