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.
12 comments:
That is a great piece of code .. Very interesting !! ..
Thanks for sharing it !!
Excellent [:)] though there are a few bugs [;D]
unsigned int fact(unsigned int n)
{
unsigned int f=n;
while(n---2)
f*n; <== should be f*=n;
return n; <== should return f;
}
@Soham: Thanks :-)
@Santosh: That's why you're Dada :-)
I've corrected the mistakes. Thanks!
(The reason for the error is obvious: Not tested. Reason for not testing: No compiler. Reason for not having compiler: I'm using Vista, which doesn't seem to support any good light weight compilers.)
Keep the posts coming dude [:)] I like to read [:)]
Harish,
What's this biz?
@Santosh,
Support like that virtually doubles my enthusiasm Thanks :-)
@Sunath uncle,
Thanks for reading and commenting.. What biz are you asking about??
Really good.
Oh you're such a nerd! :D
I don't understand much of it though!
And it's ok to be a procrastinator! BTW I'm blogrolling you, I should have done this earlier but I always thought you only write a kannada blog and it'll just take me years to read that much kaanda! :P
Harish,
I know factorials.
But I do not understand the code you have written. Is it programming
language?
@Sriram,
Thanks :-)
@Niveditha,
Being a nerd is better than being one in herd, right? ;-)
Welcome to my blog and thanks for blog-rolling me.
@Sunath uncle,
Yes, it is C programming language, which I like very much. I guess I should've mentioned it in the post. I'll do that now!
good code buddy!!!!
now i m recollecting college days.. where we r writint this prgm in Pascal!!!
ಬಾಲು, after you mentioned about Pascal, I remembered my PU days!! I've al(most/ready) forgotten Pascal!
Thanks!
Post a Comment