Q:

Given a number n, write a method that sums all multiples of three and five up to n (inclusive)

0

Given a number n, write a method that sums all multiples of three and five up to n (inclusive).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

public Integer sum(Integer n) {
Integer sum = 0;
for (int i = 1; i <= n; i++) {
    if (i % 3 == 0 || i % 5 == 0) {
        sum += i;
    }
}
return sum;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now