Member Avatar for Ajantis

Hey folks :)

I was writing some code today, and I wrote a function for computing factorials without use of recursion. What do you think about this one?

int factorial(N) {

	int r;
	r = 0;

	while (N > 0)
	{

		r *= (n-1);
		n--;
	}
	return r;

}

that it will not work as N != n

it will so something, if N != 0 and n is defined in scope for the method.
What it will do is cause an eventual numeric underflow when n drops below the minimum for its datatype :)

Of course the entire loop can be removed from this method, and the method condensed to "return 0;" which is all that the loop would ever result in if it wouldn't crash :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.