Ads by ProfitSence
Close

Program a Java Factorial Function

Last Updated on Wednesday 5th Oct 2022
  • A factorial of a number is the product of all positive integers from 1 up to the number.
  • The only exception is 0, where its factorial is 1.

Take, for example, How it would calculate the factorial of 4.

			
					4 * 3 * 2 * 1 === 24;

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

  public static void main(String[] args) {
    System.out.println("Factorial of 8 is: " + factorial(8));
  }
}

			
	
			
					Factorial of 8 is: 40320