#include <iostream>using namespace std;int main(){ bool quit = false; do { char factorialResponse = 'n'; cout << "Would you like to enter a positive number to get it's factorial? - "; cin >> factorialResponse; cout << endl; if(factorialResponse == 'y' || factorialResponse == 'Y') { int inputFactorial = 0; cout << "Please enter a positive number to get it's factorial - "; cin >> inputFactorial; int i = inputFactorial - 1; int goFactorial = inputFactorial; for(; i > 0 ; --i) { goFactorial *= i; } cout << "!" << inputFactorial << " = " << goFactorial << endl << endl; } else if (factorialResponse == 'n' || factorialResponse == 'N') { quit = true; } else { cout << "Please enter a valid choice" << endl << endl; } } while(!quit); cout << "Press any key to continue... " << endl; char response; cin >> response; return 0;}
Get the factorial of a number
Would anyone mind looking over this short code for me? It's an assignment out of a book I have that wants you to ask a user for a number and then return the factorial of that number. The program works but I am interested in knowing if I have gone about writing this code the correct way or if I should have tackled the problem differently. I am very interested if my logic for getting the factorial is the best solution. Thanks.
That is a perfectly sound way of calculating a factorial, yes. Another (arguably more elegant) way is to use recursion:
Although, while this is a little neater, it's actually less spatially-efficient due to the fact that each recursive call requires extra memory.
int factorial(int in) { if (in > 1) return (in*factorial(in-1)); else return 1;}
Although, while this is a little neater, it's actually less spatially-efficient due to the fact that each recursive call requires extra memory.
Factorial of 0 is defined as 1.
Unfortunately, due to rate of growth, using built-in types any of above methods produces accurate results only up to 12 or so when integers overflow. There is no trivial solution to that, at least not in C++.
Unfortunately, due to rate of growth, using built-in types any of above methods produces accurate results only up to 12 or so when integers overflow. There is no trivial solution to that, at least not in C++.
Of interest, but not consequence to your code is that determining a factorial is possible through template meta-programming at *COMPILE TIME:edited*.
http://en.wikipedia.org/wiki/Template_metaprogramming
*Whoopse. Turn brain on.
[Edited by - M2tM on August 19, 2010 6:30:18 PM]
http://en.wikipedia.org/wiki/Template_metaprogramming
*Whoopse. Turn brain on.
[Edited by - M2tM on August 19, 2010 6:30:18 PM]
Quote:Original post by M2tM
Of interest, but not consequence to your code is that determining a factorial is possible through template meta-programming at compile time.
Corrected your error.
Quote:Original post by diablos_bladeQuote:Original post by M2tM
Of interest, but not consequence to your code is that determining a factorial is possible through template meta-programming at compile time.
Corrected your error.
Thanks.
You either need some kind of big-num construct that won't overflow with massive factorials or, perhaps most simply, something like this:
const char * factorial_str(unsigned int i){ static const char * factorials[] = { "1","1","2","6","24","120","720","5040","40320","362880","3628800", "39916800","479001600","6227020800","87178291200","1307674368000", "20922789888000","355687428096000","6402373705728000", "121645100408832000","2432902008176640000" }; if (i >= sizeof(factorials) / sizeof(const char *)) { throw std::range_error("factorial is unknown"); } return factorials;}
Quote:Original post by dmatter
You either need some kind of big-num construct that won't overflow with massive factorials or, perhaps most simply, something like this:
*** Source Snippet Removed ***
I'm guessing that this is beyond the scope of the exercise in the book, somehow.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement