• + 4 comments

    hello, I use java and try to solve it using a recursive method

    public static void main(String[] args) {
            // TODO code application logic here
           Scanner in = new Scanner(System.in);
            int N = in.nextInt();
            System.out.println(factorial(N));
        }
    public static long factorial(int n){
            if(n<0) 
                return -1;
            else if(n==0) 
                return 1;   
                else 
                return (long) n*factorial(n-1);
        }
    

    the result is not correct, but I can't find the reason. Please help.