Compute factorial using for loop in Java

public class Factorial { 
 
  int calculate(int number) { 
    if (number < 0) { 
      throw new IllegalArgumentException("integer must be greater than zero"); 
    } 
    if( number == 0 || number == 1) 
      return 1; 
    else { 
      int total = 1; 
      for (int i = 1; i < number + 1; i++) { 
        total *= i; 
      } 
      return total; 
    } 
  } 
    public static void main(String[] args) { 
    Factorial factorial = new Factorial(); 
    System.out.println(factorial.calculate(6)); 
  } 
 
}

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published.