In this example, you will learn to write a JavaScript program to display integers in the range 1 to 100 that are divisible by 3 and 5.
But before going into the example, you must have knowledge of the following JavaScript topics:
Example 1: Display integers in the range 1 to 100 that are divisible by 3
// range 100
const range = 100;
// display numbers divisible by 3
for (let i = 1; i <= range; i++) {
if (i % 3 == 0) {
console.log(i);
}
}
Output
3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93 96 99
Here,
1. for
loop is running for a range 1 to 100.
2. If
statement inside for loop with the condition i % 3 == 0
checking on each iteration, whether a number is divisible by 3 or not.
3. Body of the if
statement will print the number if it satisfies the if condition.
Example 2: Display integers in the range 1 to 100 that are divisible by 5
// range 100
const range = 100;
// display numbers divisible by 5
for (let i = 1; i <= range; i++) {
if (i % 5 == 0) {
console.log(i);
}
}
Output
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100
Here,
1. for
loop is running for a range 1 to 100.
2. If
statement inside for loop with the condition i % 5 == 0
checking on each iteration, whether a number is divisible by 5 or not.
3. Body of the if
statement will print the number if it satisfies the if
condition.
Example 3: Display integers in the range 1 to 100 that are divisible by either 3 or 5
// range 100
const range = 100;
// display numbers divisible by 3 or 5
for (let i = 1; i <= range; i++) {
if (i % 3 == 0 || i % 5 == 0) {
console.log(i);
}
}
Output
3 5 6 9 10 12 15 18 20 21 24 25 27 30 33 35 36 39 40 42 45 48 50 51 54 55 57 60 63 65 66 69 70 72 75 78 80 81 84 85 87 90 93 95 96 99 100
Here,
1. for
loop is running for a range 1 to 100.
2. If
statement inside for loop with the condition i % 3 == 0 || i % 5 == 0
checking on each iteration, whether a number is divisible by either 3 or 5.
3. Body of the if
statement will print the number if it satisfies the if
condition.
Example 4: Display integers in the range 1 to 100 that are divisible by both 3 and 5
// range 100
const range = 100;
// display numbers divisible by 3 and 5
for (let i = 1; i <= range; i++) {
if (i % 3 == 0 && i % 5 == 0) {
console.log(i);
}
}
Output
15 30 45 60 75 90
Here,
1. for
loop is running for a range 1 to 100.
2. If
statement inside for loop with the condition i % 3 == 0 && i % 5 == 0
checking on each iteration, whether a number is divisible by 3 and 5.
3. Body of the if
statement will print the number if it satisfies the if
condition.