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
Approach 1: Using for Loop
Using n = 50 as an example, the program should output all numbers below 50 that are divisible by both 3 and 5. To do this, divide each number between 0 and n by 3 and 5 in a for loop. Print that value if the remainder in both scenarios is 0.
Example: This example describes how can we print numbers that are divisible by 3 and 5 using a for loop.
// Function for finding out
// the numbers which are
// divisible by 3 and 5
function divisibleBy3and5(n) {
for (let i = 0; i <= n; i++) {
if (i % 3 == 0 && i % 5 == 0) {
console.log(i)
}
}
}
let N = 50;
// Calling function for printing
// the numbers
divisibleBy3and5(N);
Output
0 15 30 45
Time Complexity: It is taking O(N) time as for loop will iterate n (given number) times.
Auxiliary Space: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
Approach 2: Using LCM Method
Here we are taking LCM of 3 anad 5 which is 15. If any number which is divisible by 15 it means it can also divisible by 3 and 5 so we are only searching for that number which is divisible by 15 and printing that number only.
Example: This example is showing how can you print number which are divisible by 3 and 5 using LCM
// Creating variable having
// value 50
let n = 50;
// For loop for printing
// numbers which are divisible
// by both 3 and 5
for (let i = 0; i <= 50; i++) {
// Lcm of 3 and 5 is 15
if (i % 15 == 0) {
console.log(i);
}
}
Output
0 15 30 45
Time Complexity: The time complexity will be O(n) as loop will iterate n times.
Auxiliary Space: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
Approach 3: Using the Arithmetic Series Formula
In this approach, we are going to iterate everytime with the increment of 15, as 15 is the LCM of 3 and 5, And if number is divisible by 15 then it will also be divisible by 3 and 5. And it will only work if we are starting our iteration from 0.
Example: This example describes how you can find out the numbers which are divisible by 3 and 5 using arithmetic series formula
// JavaScript program to print
// numbers that are divisible
// by 3 and 5
// Lcm of 3 and 5 is 15
// LCM(3, 5) = 15
let n = 150;
// Function for finding
// out divisible number
function divisibility(n) {
// Start loop from 0 to n
// by increment of +15 every time
for (let i = 0; i <= n; i += 15) {
console.log(i);
}
}
// Calling function divisibility
divisibility(n);
Output
0 15 30 45 60 75 90 105 120 135 150
Time Complexity: The time complexity will be O(n) as loop will iterate n times.
Auxiliary Space: The space complexity of the above code is O(1), as no extra space is required in order to complete the operation.
Approach 4: Using a while loop
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
Contact Us