1. ## Factorial function ?

Code:
```/* For all the functions below, return TRUE if the
calculation is successful, FALSE if overflow occurs
Return the calculated value in the pass by reference
parameter provided
*/

Boolean calcFactorial (int n, int* nfact)
{
unsigned long int count = 1;
unsigned long int result = 1;

while(count <= n)
{
{
return FALSE;
}
else
{
result = result * count;

count++;
}
}
return TRUE;
}```
Factorial is: n! = n * (n-1) * (n-2) * . . . * 3 * 2 * 1
Can someone please check if my coding is correct, especially the "if(*nfact <= 0)" which is checking for overflow ?

2. it won't be checking for it as nFact is a pointer to a unknown int. You either need to be putting the factorial number in *nFact or checking that result is larger than its previous value. Checking if its negative may not nessarily work.

3. Code:
```/* For all the functions below, return TRUE if the
calculation is successful, FALSE if overflow occurs
Return the calculated value in the pass by reference
parameter provided
*/

Boolean calcFactorial (int n, int* nfact)
{
unsigned long int count = 1;
unsigned long int result = 1;

while(n > 0)
{
*nfact = result;

if(*nfact <= 0)  // check for overflow ?
{
return FALSE;
}
else
{
result = result * count;

count++;
}
}
return TRUE;
}```
How is my code so far ?

4. Code:
```bool fact(int n)
{
long count= 1;
long result =1;

while(n > -1)
{
if(n==0
return 1;
else
result=result * n--;
}
return 0;
}```
i dont think u need *nfact in your code. check out my code which finds the factorial of a number where i send just a argumnet which is nothing but the actual number.

s.s.harish

5. the problem is, this is part of my assignment and im not suppose to change any parameters

6. This may or may not be what you are looking for:

Code:
```Boolean calcFactorial (int n, int* nfact)
{
int count = n;
*nfact = 1;

while(count > 0)
{
*nfact *= count;
count--;
}
return TRUE;
}

int main(int argc, char **argv)
{
int result;
int i;

printf("Input a number: ");
scanf("%d",&i);

calcFactorial(i,&result);

printf("Result of factorial was: %d",result);

return 0;
}```
Basically what it does is have a value (result) to be passed in by reference to the factorial function. Then simply start at the n and multiply this by nfact itself until count is equal to 0. I also initialised nfact to 1 in case the variable wasn't initialised previously (which would return garbage if you were to use it before it was initialised).

Also for your original code, you didn't do anything useful in where you stored the calculations. Storing it in result which only has a local scope in the function means that the value will "disappear" after the function loop ends. You could just return result be since you need to have the same parameters as what you specified before this is not really feasible.

7. Originally Posted by 0rion
You could just return result be since you need to have the same parameters as what you specified before this is not really feasible.
The return value is supposed to be used to determine whether overflow would have occurred, and thus whether any value calculated is valid or not.

I thought this sounded familiar.

8. Code:
```Boolean calcFactorial (int n, int* nfact)
{
*nfact = 1;

while(n > 0)
{
*nfact = *nfact * n;
n--;
}

if(*nfact < 0x7fffffff)
{
return TRUE;
}
else
{
return FALSE;
}
}```
Thank you Orion. Modify it a bit, but works great.

9. Not if you want to detect overflow.
Code:
```#include <stdio.h>

typedef enum { FALSE, TRUE } Boolean;

Boolean calcFactorial (int n, int* nfact)
{
*nfact = 1;

while(n > 0)
{
*nfact = *nfact * n;
n--;
}

if(*nfact < 0x7fffffff)
{
return TRUE;
}
else
{
return FALSE;
}
}

int main()
{
int value, result;
for ( value = 0; value < 20; ++value )
{
if ( calcFactorial(value,&result) )
{
printf("%2d! = %d\n", value, result);
}
}
return 0;
}

/* my output
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
17! = -288522240
18! = -898433024
19! = 109641728
*/```
An expected output would be:
Code:
``` 0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600```

10. The overflow detecting part i can worry later. Right now i just want to work on the correctless of the calculation parts. I'm currectly working on my other functions that work together with this function. Hope all goes well.

11. Originally Posted by koolguysj
The overflow detecting part i can worry later. Right now i just want to work on the correctless of the calculation parts.
You realize that overflow detection is directly related to the correctness?

12. Heres an one that checks for overflow that uses the implementation from the other forum you posted in:

Code:
```#include <limits.h>
#include <stdio.h>

// your other definitions goes here..

Boolean calcFactorial (int n, int* nfact)
{
int count = n;
*nfact = 1;

while(count > 0)
{
if (*nfact >= INT_MAX / count)
return FALSE;
*nfact *= count;
count--;
}
return TRUE;
}

int main(int argc, char **argv)
{
int result;
int i;
int j;

printf("Input a number: ");
scanf("%d",&i);

j = calcFactorial(i,&result);

if (j == FALSE)
{
fprintf(stderr,"Sorry the input was too large!");
return 1;
}

printf("Result of factorial was: %d",result);

return 0;
}```
You simply just have to check the return value and see if it overflowed or not, so if it overflowed then you simply can print a error message to the user saying it was too big or something (what ever you want to do with it).