JavaScript: Sum the multiples of 3 and 5 under 1000
JavaScript Conditional Statement and loops: Exercise-12 with Solution
Sum of Multiples of 3 and 5 under 1000
Write a JavaScript program to sum 3 and 5 multiples under 1000.
Sample Solution
JavaScript Code:
// Variable to store the sum of numbers divisible by 3 or 5
var sum = 0;
// Loop through numbers from 0 to 999
for (var x = 0; x < 1000; x++) {
// Check if the current number is divisible by 3 or 5
if (x % 3 === 0 || x % 5 === 0) {
// Add the current number to the sum
sum += x;
}
}
// Output the final sum
console.log(sum);
Output:
233168
Flowchart:
Live Demo:
See the Pen javascript-conditional-statements-and-loops-exercise-12 by w3resource (@w3resource) on CodePen.
Improve this sample solution and post your code through Disqus.
Previous: Write a JavaScript program to compute the greatest common divisor (GCD) of two positive integers.
Next: javascript Event Handling Exercises Home
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/javascript-exercises/javascript-conditional-statements-and-loops-exercise-12.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics