Skip to content
Advertisement

Need help to calculate the sum of numbers by omitting somes of them

#1. I need to write a Java program that calculates the sum of numbers from 1 through 10,000 (including 1 and 10,000) but omitting numbers that are divisible by three and numbers whose hundred digit is 2 or 3 (for example 8200 or 5312).

I begin with but didnt work:

JavaScript

Advertisement

Answer

You should only increment the sum after testing the condition.

JavaScript

Using a Stream can simplify this:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement