← Unit 2 · all exercises
Saved in this browser only

Add up the numbers

Write a method that adds up every whole number from 1 up to and including n, and returns the total. If n is less than 1, return 0.

int sumTo(int n)

Solution.java
Loading editor…
Results

Examples

  • sumTo(10) → 551 + 2 + ... + 10
  • sumTo(1) → 1the smallest ordinary case

There are also 5 hidden tests you can't see. Solve the problem, not the examples.