| | |
Fibonacci Series
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2006
Posts: 6
Reputation:
Solved Threads: 0
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
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
A search for Fibonacci here or with Google ought to give you enough to start with, if not something to finish with.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
This is one way of going about it without much thinking
fn = [ Phi^ n - (-Phi)^-n ] / (2Phi-1)
for smaller values of n you need approximation.
Phi = ( 5^½ + 1 ) / 2 = 1.6180339 (the golden ratio)
^ = power
But this method is seldom used. This is more mathematical. Use Google as Dave suggested. You can get lots of material.
fn = [ Phi^ n - (-Phi)^-n ] / (2Phi-1)
for smaller values of n you need approximation.
Phi = ( 5^½ + 1 ) / 2 = 1.6180339 (the golden ratio)
^ = power
But this method is seldom used. This is more mathematical. Use Google as Dave suggested. You can get lots of material.
•
•
Join Date: Jul 2006
Posts: 6
Reputation:
Solved Threads: 0
G'day
where error?
1 error(s), 0 warning(s)
where error?
1 error(s), 0 warning(s)
C++ Syntax (Toggle Plain Text)
# include <iostream.h> int fibonacci(int n) {<blockquote> int x1 = 0, fib; int x2 = 1; if(n >= 1) {<blockquote> for(int i=2;i<= n; i++) { </blockquote></blockquote><blockquote><blockquote><blockquote>fib = x1+ x2; x1 = x2; x2 = fib; </blockquote></blockquote></blockquote><blockquote><blockquote> } return fib; </blockquote></blockquote><blockquote> } </blockquote>
Last edited by WolfPack; Aug 5th, 2006 at 7:58 am.
Try to append this to your code
C++ Syntax (Toggle Plain Text)
} int main() { cout<<fibonacci(/*some number*/); }
The key to eliminating bugs from your code is learning from your mistakes.
> where error?
Please use the code tags when posting code - can't you read this
http://www.daniweb.com/techtalkforum...cement8-3.html
Or see the background image in the compose message window?
Also, count the { and }
I count 3 { and only 2 }
Please use the code tags when posting code - can't you read this
http://www.daniweb.com/techtalkforum...cement8-3.html
Or see the background image in the compose message window?
Also, count the { and }
I count 3 { and only 2 }
•
•
Join Date: Jul 2006
Posts: 6
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#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).
and
Last edited by Dave Sinkula; Aug 5th, 2006 at 3:51 pm.
•
•
•
•
Originally Posted by kookai
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).
(2) Surely you can make an attempt at these changes.
Please enclose posted code in code tags -- just like it says in the little box you reply in.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jan 2009
Posts: 2
Reputation:
Solved Threads: 0
Here is the shortest code for finding the fibo series
#include <iostream>
using namespace std;
int main(){
long a;
cout << "Enetr Limit you want\n\n";
cin >> a;
cout << "Fibonachi numbers < " << a << endl;
long f = 0, f2, f1 = 1;
while(true) {
f2 = f + f1;
if(f2 > a) {
break;
}
cout << " " << f2 << endl;
f = f1;
f1 = f2;
}
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main(){
long a;
cout << "Enetr Limit you want\n\n";
cin >> a;
cout << "Fibonachi numbers < " << a << endl;
long f = 0, f2, f1 = 1;
while(true) {
f2 = f + f1;
if(f2 > a) {
break;
}
cout << " " << f2 << endl;
f = f1;
f1 = f2;
}
system("pause");
return 0;
}
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: <Non-trivial> overloading operator []
- Next Thread: Compiler Errors, Warning, Differences Help
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






