Java for loop calculate factorial

Question

We would like to calculate factorial using for-loop for number 6.

In mathematics, the exclamation point (!) means factorial.

A factorial is written as 3!.

For example, 3! is 1 times 2 times 3, which is 6.

And 5! is 1 times 2 times 3 times 4 times 5, which is 120.

public class Main {

  public static void main(String[] args) {
    int n = 6;

    //your code here
  }
}


public class Main {

  public static void main(String[] args) {
    int n = 6;

    int factorial = 1;
    for (int i = 1; i <= n; i++) {
      factorial *= i;
    }

    System.out.println(n + "! is " + factorial);
  }
}



PreviousNext

Related