Factorial in Java - recursive



fact rec en

import java.util.Scanner;

public class Fact_rec {
  public static void main(String[] args) {
    int n;
    Scanner key=new Scanner(System.in);
    System.out.print("n = ");
    int f=factorial(n);
    System.out.print("! = ");

  static int factorial(int n){
      return n*factorial(n-1);
      return 1;    


  In the first line of the program, we inform you that we will use the Scanner class from the util java library. The Scanner class will be used to load input data in our program. In java, program execution starts with calling the public main() function contained in the class whose name matches the program name (the program must be saved in the Fact_rec.java file and then compiled to the Fact_rec.class file). The definition of the Fact_rec class starts in the third line of the program. Line 4 contains the beginning of the main function definition, this is the beginning of our algorithm execution (START block (1)). In the next line, we declare a variable of integer type n, which we will use in a moment to load the argument of function factorial from the keyboard. In java, variables can be declared anywhere before using them, so we do not declare the result variable immediately. On the 6th line, we declare and initialize the key variable as a Scanner object. We indicate that the source of text data will be the System.in stream, or keyboard.

  The lines from 7 and 8 perform the input/output operation (2) in which the input data is taken - n, the number for which we want to calculate the value of the function factorial.

 In line 9, we declare a variable result and assign to it the result of the function factorial called function with the argument n.

 In lines 10 to 11, we print the result obtained (input-output block (6)).

 The curly brace } from line 12 completes the main function and also the operation of the entire program (end block of the algorithm (7)).

 In line 15, we declare the function factorial we used in the main function. A function that is a method of the same class does not have to be declared beforehand before use. The function factorial takes one integer argument (n) and returns the integer as a result. Between the logical brackets { from the end of the line 15 and } on the line 20 is the body of the function - a set of instructions executed by the program after calling this function.

  In line 16, we check if n is greater than 1 - block (3) of the algorithm. If so, in line 17, we determine the result of the function as n multiplied by the value of the function factorial (the same function as the one we currently define) for n minus 1 (4). At this point, the function factorial calls itself. This function is called a recursive function. If n is not greater than 1, then in line 19 we set the result of the function equal to 1 (ending condition of recursion) - block (5).

 The curly brace } from line 20 ends the function factorial, while the curly bracket in line 21 ends the definition of the Fact_rec class.