can anybody write me the fibonacci series and factorial of a number program in c++
i tried a lot but couldn't get throug
I searched google for C code for the Fibonacci series and I found this:
You can easily modify it for your needs, and search google for the factorial of a number in c/c++Code:Program to generate Fibonacci Series #include <stdio.h> void main() { int OldNum, NewNum, FibNum, MaxNum; printf(" Generate Fibonacci Numbers till what number? "); scanf("%d", &MaxNum); OldNum=0; NewNum=1; FibNum = OldNum + NewNum; printf("%d, %d, %d, ", OldNum, NewNum, FibNum); for(;;) { OldNum = NewNum; NewNum = FibNum; FibNum = OldNum + NewNum; if(FibNum > MaxNum) { printf(" "); exit(1); } printf("%d, ", FibNum); } }
█ Xavier L | Community Public Relations Manager (Free Hosting Support)
█ Yes, my position is too cool to even exist!
█ How am I helping? Rate this post by clicking theicon below! (this is even better than "liking" a post)
█ Terms of Service | Acceptable Use Policy | x10Hosting Wiki
Here is an example of the above code ported to C++ (with minor changes...)
Code:#include <iostream> using namespace std; void main() { unsigned int oldValue, newValue, fibNum, maxNum; // initialize value... oldValue = 0; newValue = fibNum = 1; cout << "Generate Fibonacci Numbers till what number?" << endl; cin >> maxNum; // display initial values... cout << oldValue << ", " << newValue << ", " << fibNum; // Now to calculate up to maxNum... while (newValue + fibNum < maxNum) { oldValue = newValue; newValue = fibNum; fibNum = oldValue + newValue; // change to output comma before fibNum vice always having it at the end cout << ", " << fibNum; } }
Code:#include <iostream> using namespace std; int main() { unsigned int factorial = 1; unsigned int number; cout << "Calculate the factorial of a number." << endl; cout << "Enter a positive integer." << endl; cin >> number; for(int i=1; i <= number; i++) { factorial *= i; } cout << "The factorial of " << number << " is " << factorial << "." << endl; return 0; }
Real programmers don't document their code - if it was hard to write, it should be hard to understand.
This is a homework assignment, isn't it? Shame on you for being such a lazy cheat you couldn't even be bothered to google for answers. If you really want to know about factorial and fibonacci algorithms, read section 1.2 of "Structure and Interpretation of Computer Programs" (SICP), especially section 1.2.1 for factorial and section 1.2.2 and problem 1.19 for fibonacci numbers. Problem 1.19 deals with an efficient method of computing fibonacci numbers in O(log n) (logarithmic) time rather than O(n) (linear) time.
As for the rest of you, shame on you for giving the answers away. Shame, I say.
Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.Misson, not Mission.
█ Xavier L | Community Public Relations Manager (Free Hosting Support)
█ Yes, my position is too cool to even exist!
█ How am I helping? Rate this post by clicking theicon below! (this is even better than "liking" a post)
█ Terms of Service | Acceptable Use Policy | x10Hosting Wiki