fork download
  1.  
  2. import java.util.*;
  3. import java.lang.*;
  4. import java.io.*;
  5.  
  6. /* Name of the class has to be "Main" only if the class is public. */
  7. class Ideone
  8. {
  9. public static int sumNumbersRecursive(int n) {
  10. if (n == 0) {
  11. return 0;
  12. } else {
  13. return n + sumNumbersRecursive(n - 1);
  14. }
  15. }
  16.  
  17. public static void main(String[] args) {
  18. int n = 5; // Replace 5 with the desired value of N
  19. int result = sumNumbersRecursive(n);
  20. System.out.println("The sum of numbers from 0 to " + n + " is: " + result);
  21. }
  22.  
  23.  
  24. }
Success #stdin #stdout 0.15s 57516KB
stdin
Standard input is empty
stdout
The sum of numbers from 0 to 5 is: 15