Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

recursion in java

class scratch{
    public static long factorial(int n){
        if ( n == 1 ){
            return 1;
        }
        else{
            return n * factorial( n - 1 );
        }
    }
    public static void main(String[] args) {
        System.out.println( factorial(5) );
        //Print 120 (5!) to the console
    }
}
 
PREVIOUS NEXT
Tagged: #recursion #java
ADD COMMENT
Topic
Name
4+6 =