Factorial Recursion

April 11, 2016, by Hamid Siddiqui

/**
 * @author Hamid Siddiqui
 * https://github.com/inventionsbyhamid
 * https://www.quora.com/profile/Hamid-Siddiqui
 *
 * @program To find factorial of a number using recursion
 * Example: 5! = 1*2*3*4*5
 */

import java.io.*;

class Factorial_Recursion {

    public static void main(String args[]) throws IOException {

        BufferedReader k = new BufferedReader(new InputStreamReader(System.in));

        System.out.println("Enter number");
        int num = Integer.parseInt(k.readLine());

        long factorial = factorial(num);
        System.out.println("Factorial = " + factorial);

    }
    static long factorial(int num){

        if(num == 0)//base condition
            return 1;
        return num*factorial(num-1);
    }
}
Sample Input/Output

Enter number
6
Factorial = 720




CodeStalk →

← Create file