+ Reply to Thread
Results 1 to 7 of 7

Thread: fibonacci series and factorial

  1. #1
    dhruv227 is offline x10 Lieutenant dhruv227 is an unknown quantity at this point
    Join Date
    Dec 2008
    Location
    da capital
    Posts
    390

    fibonacci series and factorial

    can anybody write me the fibonacci series and factorial of a number program in c++
    i tried a lot but couldn't get throug

  2. #2
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: fibonacci series and factorial

    I searched google for C code for the Fibonacci series and I found this:
    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);
       }
    }
    You can easily modify it for your needs, and search google for the factorial of a number in c/c++
    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 the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

  3. #3
    Submariner is offline x10Hosting Member Submariner is an unknown quantity at this point
    Join Date
    Dec 2007
    Location
    TN, USA
    Posts
    44

    Re: fibonacci series and factorial

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

  4. #4
    dhruv227 is offline x10 Lieutenant dhruv227 is an unknown quantity at this point
    Join Date
    Dec 2008
    Location
    da capital
    Posts
    390

    Re: fibonacci series and factorial

    can anybody write it for finding the factorial of a number ?

  5. #5
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: fibonacci series and factorial

    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.

  6. #6
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: fibonacci series and factorial

    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.

  7. #7
    xav0989's Avatar
    xav0989 is offline Community Public Relation xav0989 is just really nice
    Join Date
    Jul 2008
    Location
    ifk
    Posts
    4,438

    Re: fibonacci series and factorial

    Quote Originally Posted by misson View Post
    As for the rest of you, shame on you for giving the answers away. Shame, I say.
    I amused myself once, writing code to create the fibonacci series to a certain point. Anyways, Misson, you should become the homework checker. If you spot a question that looks like it's homework, simply warn us, and we'll only give hints!
    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 the icon below! (this is even better than "liking" a post)
    Terms of Service | Acceptable Use Policy | x10Hosting Wiki

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers