How to useLCM Method in Javascript
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.
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