The factorial function (symbol: !) means to multiply a series of descending natural numbers. Examples:
4! = 4 × 3 × 2 × 1 = 24
7! = 7 × 6 × 5 × 4 × 3 × 2 × 1 = 5040
import java.util.Scanner; public class Factorial { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter any number: "); int i = in.nextInt(); System.out.print(i + "! is " + factorial(i)); } private static int factorial(int n) { if(n <= 0) { return (1); } else { return (n * factorial(n-1)); } } }
Output:
Enter any number: 4 4! is 24
Hello, I viewed my site on Opera and it’s working well.