How to usea while loop in Javascript
We can iterate through the numbers starting from 0 and increment by 1 until the given number (inclusive). For each number, we check if it is divisible by both 3 and 5. If it is, we print it.
Example:
function printDivisibleBy3And5(n) {
let num = 0;
while (num <= n) {
if (num % 3 === 0 && num % 5 === 0) {
console.log(num);
}
num++;
}
}
// Test cases
printDivisibleBy3And5(75);
printDivisibleBy3And5(150);
Output
0 15 30 45 60 75 0 15 30 45 60 75 90 105 120 135 150
JavaScript Program to Print All Numbers Divisible by 3 and 5 of a Given Number
In this article, we are going to discuss how to print all the numbers that are divisible by 3 and 5. We will write a program for finding out those numbers. For this, we can use loop or conditional statements.
Example:
Input: 75
Output: 0, 15, 30, 45, 60, 75
Input: 150
Output: 0, 15, 30, 45, 60, 75, 90, 105, 120, 135, 150
We can solve this by using the following methods:
Table of Content
- Using for Loop
- Using LCM Method
- Using the Arithmetic Series Formula
- Using a while loop
Contact Us