Now let's see how we can calculate factorial using the while loop. Here's our modified method: public static int getFactorialWhileLoop(int n){ int result = 1; while (n > 1) { result = result * n; n -= 1; } return result; } This is pretty similar to the for loop. Except ...
The weakness of this design from a Java perspective should be obvious: it does not allow us to select the algorithm we wish to use at runtime, which was a major design feature we were striving for. (I mean, after all, if our new program has different constraints, we should be able t...
In the following PHP program factorial of number 5 is calculated. This is a simple program using for loop. This for loop is iterated on the sequence of numbers starting from the number till 1 is reached. Code: <?php //example to calculate factorial of a number using simple for loop //...