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

Sequence

Recursive sum

Core exercise

Write sommeRecursive(int n) that computes 1 + 2 + … + n recursively. No loop.

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(sommeRecursive(100));
    }

    public static int sommeRecursive(int n) {
        // TODO : un cas d'arret, puis un appel recursif
        return 0;
    }
}

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

Hint 1

The base case (AB-SO-LUTE-LY ESSENTIAL): when n is 0, the sum is 0. Otherwise: n + sommeRecursive(n - 1).