# Why zeros?

• 04-01-2005
dalibi
Why zeros?
Code:

```I attempted to post this same request earlier but i am not sure if it went through. I appologise for dublication but, I need help please. // factorial program #include <iostream> using namespace std; int factorial(int num); int main(){ int n; cout << "Enter a number and press ENTER: " cin >> n; cout << " Factorial of this number is: " << factorial(n); return 0; } int factorial(int n){ int i; int fact = 0; for(i = 0; i <= n; i++) fact = fact * i; return fact; } although the program runs without an error message it produces zero for a result.```
• 04-01-2005
Quantum1024
The first time thru the loop i is zero and when multiplying by zero the result is zero. In subsequent runs thru the loop fact is zero meaning multiplying fact by i produces zero everytime.
• 04-01-2005
samGwilliam
You really should be looping from the n value, down, too...
• 04-01-2005
CodeMonkey
I vote for recursion: (by herbert Schildt)
Code:

```int factorial(int n) { int answer; if(n==1) return 1; answer = factorial(n - 1) * n; return answer; }```
• 04-01-2005
samGwilliam
Quote:

Originally Posted by CodeMonkey
I vote for recursion: (by herbert Schildt)
Code:

```int factorial(int n) { int answer; if(n==1) return 1; answer = factorial(n - 1) * n; return answer; }```

Good call!
• 04-01-2005
Dave Evans
Quote:

Originally Posted by dalibi
I attempted to post this same request earlier but i am not sure if it went through. I appologise for dublication but, I need help please.

Look at your code. Run through a loop or two by hand. Start with zero, multiply by anything you want to: result will always be zero. Right?

It wouldn't, if you change this:
Code:

```int fact = 0; for(i = 0; i <= n; i++)```
to this:

Code:

```int fact = 1; for(i = 1; i <= n; i++) /* actually could use i = 2; i <= n; i++ */```
Regards,

Dave
• 04-01-2005
grib
Note also that this is basically the reason that 0! = 1, so that the recursive definition makes sense N! = N*(N-1)! Without a base, we would have no idea about the value of any factorial.
• 04-01-2005
dalibi
Thank you all very much. I appreciate it.
• 04-01-2005
Dave Evans
Quote:

Originally Posted by CodeMonkey
I vote for recursion: (by herbert Schildt)
Code:

```int factorial(int n) { int answer; if(n==1) return 1; answer = factorial(n - 1) * n; return answer; }```

If you want to illustrate use of recursion, this might be a place to do it since this is relatively easy to understand (unlike lots of examples of recursion). Unfortunately this program has a bug that gives no answer for n = 0. What do I mean "no answer". Well run it.

How you you debug it? Put something like this in the function:
Code:

```int factorial(int n) {   int answer;   if(n==1) {     return 1;   }   printf("In factorial(): n = %d\n", n);   answer = factorial(n - 1) * n;   return answer; }```
Now run it with n = 0.
Of course it is easy enough to fix it to give the correct answer for n = 0.

One reason that recursive functions are sometimes hard to debug:
the program just runs and runs and runs until stack space is totally consumed, then the program exits ungracefully. (Sometimes leaving dynamically allocated memory just hanging there and unobtainable until you reboot your computer.)

Maybe beginners are better off with iteration. Recursion can come later, when it is really appropriate.

Regards,

Dave
• 04-01-2005
CodeMonkey
That's what I get for using another's code.
Code:

```int factorial(int n) { int answer; if(!n || n == 1) return 1; else if(n < 0) exit(EXIT_FAILURE); //or throw exception answer = factorial(n-1) * n; return answer; }```
• 04-01-2005
Dave Evans
Quote:

Originally Posted by CodeMonkey
That's what I get for using another's code.

More specifically: that's what you get for using Herbert Schildt's code.

Regards,

Dave