Advertisements
Write a class called math. It is to have one property called num. It also has one method called factorial. This method is to start at 1 and multiply all of the integers to num. If num is 5 then you would multiply 12345. How would you write this as a loop? Would I need to use the for loop or do/while loop?
PHP Factorial Example:
<?php $n = 5; $i = 1; $f = 1; while($i<$n){ $i = $i + 1; $f = $f * $i; } echo $f; ?>
Here is a java example that finds the factorial of a number.
public class math{ public static long factorial(int n){ if(n <= 1) return 1; else return n * factorial(n - 1); } public static void main(String [] args){ int num=5; System.out.println(factorial(num)); } }