factorial recursive function - Java java.lang

Java examples for java.lang:Math Algorithm

Description

factorial recursive function

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int n = 2;
        System.out.println(factorial(n));
    }/*  w w w  .  j  a v a2  s. c  om*/

    public static int factorial(int n) {
        if (n == 0)
            return 1;
        return n * factorial(n - 1);
    }
}

Related Tutorials