aNt17017 said:
This is my complete setup:
1 - get data from user
2 - calculate factorial
and it's still doesn't work as I wish. I don't wont to use other
libraries 'cause it should base on "simple" recursive formula.
void factorial(void)
{
clearScreen();
printf("\n\t\tCalculate the Factorial of entered numbers.\n");
printf("\t\t===========================================\n");
int num;
printf("\t\tPlease enter a value in the range of 0 to 100(q to
quit): ");
while (scanf("%d",&num) == 1)
{
if(num <= 0)
printf("\t\tPlease enter a value greater than 0.\n");
else if (num > 100)
printf("\t\tPlease keep the value under 100.\n");
else
{
printf("\t\t%d factorial = %ld\n",num, fact(num));
}
printf("\t\tPlease enter a value in the range of 0 to 100(q to
quit): ");
}
pressEnter();
}
long long fact(int n)
{
if (n == 0)
return(1);
if(n > 100)
{
printf("\t\tERROR: %d is too large for factorial.\n", n);
return 1;
}
return(n * fact(n-1));
}
Again, read <
http://cfaj.freeshell.org/google/>.
This is not a complete program. You don't have a main() function, and
you don't have the "#include <stdio.h>" that's required for printf and
scanf.
You say it doesn't work as you wish. What does it do? Or are we
supposed to guess?
What are clearScreen() and pressEnter()? It's obvious from their
names what they're supposed to do, but why on Earth do you want to
clear the screen before asking for input? If I'm running your
program, I might have important information on my screen.
In factorial(), you declare "int num;" after several call statements.
In C90, all declarations must precede all statements in a block. (C99
allows them to be mixed, as does C++, and some compilers allow it as
an extension, but there's no real benefit in using the feature here.)
You call fact() from factorial(). At the point of the call, the
declaration of the fact() function hasn't been seen yet. In C90, this
causes the compiler to assume that fact() returns an int; since it
actually returns long long, the result is undefined behavior. If
you're lucky, the program will crash; if you're unlucky, you may get
seemingly valid output in some cases. The simplest way to fix this is
to move the definition of fact() above the definition of factorial().
You can also provide separate declarations at the top of the program,
and give the definitions in any order you like (make sure the
declarations match the definitions).
fact() returns long long int (using unsigned long long int would give
you one additional bit), but you print its result using printf's "%ld"
format, which expects a long int. The format for printing a long long
int is "%lld" (or "%llu" for unsigned long long).
If you fix these problems, your program should be able to handle
values up to fact(20), or 2432902008176640000, which fits in 62 bits.
fact(21), or 51090942171709440000, requires 66 bits.
Some interesting numbers:
2432902008176640000 fact(20)
9223372036854775807 2**63-1
18446744073709551615 2**64-1
51090942171709440000 fact(21)
2**63-1 is likely to be the maximum value of a long long; 2**64-1 is
likely to be the maximum value of an unsigned long long.