Q:

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

belongs to collection: java exercises (easy level )

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)

Write a method that calculates the factorial of a ... >>
<< Sum of Two Numbers in java...