Code for Factorial, without Recursion
After having been declared to be an impregnable procrastinator by all my close-circle friends, after being connoted to update it by those who I didn't even know were reading my blog and after being told "Enough is enough, now update it buddy" by a few friends, I am in the same situation as the Indian government after the Mumbai attacks. I have no other go than updating it, at least for the sake of hushing them up till the next terror attack near my house. So here I go...
As you might already be knowing, I prefer calling programming an art to calling it a process. That's the reason I enjoy it.
Coming to the point, my friend cum colleague was trying to write a program for generating Pascal Triangle. I decided to play along and wrote a code. In the process, I wrote a function to calculate factorial for a number in an unusual way, without recursion.
Generally an incremental loop is used from 2 to N. But I used decrementing loop and here is the outcome (Code written in C programming language):
Main thing I wanted to convey in this code is the while loop, which uses the unusual "--" & "-" operator pair.
It works similar to this loop:
Not a great piece of code, but I found it to be a little interesting. Hence the post.
As you might already be knowing, I prefer calling programming an art to calling it a process. That's the reason I enjoy it.
Coming to the point, my friend cum colleague was trying to write a program for generating Pascal Triangle. I decided to play along and wrote a code. In the process, I wrote a function to calculate factorial for a number in an unusual way, without recursion.
Generally an incremental loop is used from 2 to N. But I used decrementing loop and here is the outcome (Code written in C programming language):
unsigned int fact(unsigned int n)
{unsigned int f=n;}
while(n---2)f*=n;return f;
Main thing I wanted to convey in this code is the while loop, which uses the unusual "--" & "-" operator pair.
It works similar to this loop:
for(f=n;n>=2;n--)f=f*n;
Not a great piece of code, but I found it to be a little interesting. Hence the post.