+ 5
You can either use iteration statements or recursion for getting the factorial of a number. * Using for loop to get the factorial of 5 int n = 5, fact = 1; for (int c = 1; c <= n; c++) fact = fact*c; System.out.println("Factorial of "+n+" is = "+fact); * Using Recursion : function calling itself! static int fact(int n) { int output; if(n==1) { return 1; } output = fact(n-1)*n; return output; } public static void main(String[] args) { System.out.print(fact(5)); }
5th Oct 2017, 3:13 PM
Dev
Dev - avatar