Version without JavaScript, read-only: workshops, quizzes and running code are not available here. Full version · Catalogue · Sign in

Sequence

Factorial

Core exercise

Write factorielle(int n) that returns n! as a long. By convention, 0! = 1.

A workshop goes here: you write and run code in it. It requires JavaScript.

Starting code

Exercices.java

public class Exercices {

    public static void main(String[] args) {
        System.out.println("5! = " + factorielle(5));
        System.out.println("20! = " + factorielle(20));
    }

    public static long factorielle(int n) {
        // TODO
        return 0;
    }
}

The core exercise is validated in the workshop, which requires JavaScript.

Hint 1

An int overflows from 13! onwards: that is why we return a long. Remember to initialise the accumulator to 1, not 0.