fibonacci series and factorial

dhruv227

New Member
Messages
390
Reaction score
1
Points
0
can anybody write me the fibonacci series and factorial of a number program in c++
i tried a lot but couldn't get throug
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
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++
 

Submariner

New Member
Messages
44
Reaction score
1
Points
0
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;
	}
}
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
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;
}
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
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.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
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! :biggrin:
 
Top