hi im learning C ( with turbo C++ 4.5 )
our teacher ask us if any one can bring a simple source code of
factorial of 20 digit number i mean ( its over a belion number i think )
he asked us only C code not php or VB or anything else
so if any one can write such a program please help me with it
and i dont need any guide i need the program :D of course i need the headers to becuse i`m new to c and i want to learn the code
( headers like %26lt;stdio.h%26gt; and ...%26gt;
i also found a code in the internet but its a calculator and it has geraphic on it if any one want to watch it go to
http://arashmidos2006.googlepages.com/Ca...
i didnt understand it
ok guys im realy depending on u please help me with this
Plz help Write a program to calculate (20 number) ! ( factorial) with c?
hmm a 20 digit number would be X * x-1 * x-2... x-19
I guess the big question is, does it ONLY do 20 digit numbers? heheh. been a while since i did c, i'm a c# dev... but here goes. Not even sure if a 'long' would hold your result or not...
this won't work if a 'long' won't hold the result, it may overflow.
void main()
{
long value;
long result;
cout %26lt;%26lt; "Enter your number";
value %26lt;%26lt; cin;//can't remember if you can do this in c or not...
result = value;
if(value %26gt; 1)
{
while(value %26gt; 1)
{
value = value - 1;//decrement value
result = result * value;//multiply result by value
}
cout %26lt;%26lt; result;
}
else
{
cout %26lt;%26lt; "Must be a positive whole number greater than 1";
}
}
You know, thinking more about it, i don't think a long will hold the results... the Best way to do it would be a bit array and use binary multiplication. (can't say i remember how to do binary multiplication but it was something we covered in school...)
basically make a bit array that holds enough bits to calculate 99999999999999999999! and do binary math the same as the above code. :) good luck
Reply:Sorry about that. I fixed the problem:
#include %26lt;iostream%26gt;
// Need the climits file in order to know the maximum size of an unsigned
// long int.
#include %26lt;climits%26gt;
// Declare the class.
class Factorial {
public:
Factorial(unsigned short num = 1);
bool inRange();
unsigned long getFactorial();
private:
unsigned short num;
}; // End of class.
// Implement the methods:
Factorial::Factorial (unsigned short num) {
this-%26gt;num = num;
}
bool Factorial::inRange() {
// Maximum possible value for validation.
unsigned long max = ULONG_MAX;
// Loop from 1 to num, dividing the number from max.
for (int i = num; i %26gt;= 1; --i) {
max /= i;
} // End of the for loop.
// Return a true/false value.
if (max %26lt; 1) {
return false;
} else {
return true;
}
} // End of the factorialInRange() function.
unsigned long Factorial::getFactorial() {
// For the factorial.
unsigned long sum = 1;
// Loop from 1 to num, adding the result to the sum.
for (int i = 1; i %26lt;= num; ++i) {
// Multiply current sum times i.
sum *= i;
} // End of the for loop.
return sum;
} // end of the returnFactorial() function.
// Start the main() function.
int main() {
// Declare the variable for the user input.
unsigned short numberIn = 0;
// Prompt the user, take, and validate the input.
std::cout %26lt;%26lt; "Enter a small, positive integer: [##] ";
while (!(std::cin %26gt;%26gt; numberIn) || ((numberIn %26lt; 1) || (numberIn %26gt; 20))) {
// Problem! Clear cin and reprompt.
std::cin.clear();
std::cin.ignore(100, '\n');
std::cout %26lt;%26lt; "I said, 'Please enter a positive integer.' Please: [##] ";
} // End of WHILE.
// Discard any extraneous input.
std::cin.ignore(100, '\n');
// Create the object.
Factorial f(numberIn);
// Print the results.
if (f.inRange()) {
std::cout %26lt;%26lt; "The factorial of " %26lt;%26lt; numberIn %26lt;%26lt; " is "
%26lt;%26lt; f.getFactorial() %26lt;%26lt; ".\n\n";
} else {
std::cout %26lt;%26lt; "The factorial of " %26lt;%26lt; numberIn
%26lt;%26lt; " cannot be calculated.\n"
%26lt;%26lt; "Use a smaller number.\n\n";
}
std::cout %26lt;%26lt; "Press enter. \n";
std::cin.get();
return 0;
} // End of the main() function.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment