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
publicclassExercices {
publicstaticvoid main(String[] args) {
System.out.println(sommeRecursive(100));
}
publicstaticint sommeRecursive(int n) {
// TODO : un cas d'arret, puis un appel recursifreturn0;
}
}
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).