JavaScript Program to Print All Numbers Divisible by 3 and 5 of a Given Number
Last Updated :
03 Jun, 2024
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:
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.
JavaScript
// 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);
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
JavaScript
// 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);
}
}
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.
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
// 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);
Output0
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:
JavaScript
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);
Output0
15
30
45
60
75
0
15
30
45
60
75
90
105
120
135
150
Similar Reads
JavaScript Program to find All Divisors of a Natural Number
In this article, we are going to implement a program through which we can find the divisors of any natural number. Divisors refer to the numbers by which we can divide another number, resulting in zero remainder. These divisors are also commonly referred to as factors. Example: Input: 14Output: 1,2,
2 min read
JavaScript Program to Print All Prime Numbers in an Interval
In this article, we will explore a JavaScript program to find and print all prime numbers within a given range. A prime number is a natural number greater than 1, which is only divisible by 1 and itself. The first few prime numbers are 2 3 5 7 11 13 17 19 23â¦, The program efficiently identifies and
4 min read
JavaScript Program to Find All Divisors of a Number
In this article, we will demonstrate different approaches to writing a JavaScript Program to Find all Divisors of a Number. We will have an input number and print all the divisors of that number in the form of a resultant array. Methods to Find All Divisors of a NumberTable of Content Method 1: Naiv
2 min read
JavaScript Program to Find Smallest Number that is Divisible by First n Numbers
Given a number, our task is to find the smallest number that is evenly divisible by the first n numbers without giving any remainder in JavaScript. Example: Input: 10Output: 2520 Explanation: 2520 is smallest number which is completely divisible by numbers from 1 to 10. Below are the following appro
4 min read
JavaScript Program for Division of Two Numbers
In this article, we will see the basic approaches for the division of two numbers in JavaScript. The division is used in many languages, In JavaScript it can be done using some operators or some other methods. Javascript has no data type such as integer, float, or double so we can directly use them
3 min read
JavaScript Program to Multiply the Given Number by 2 such that it is Divisible by 10
In this article, we are going to implement a program through which we can find the minimum number of operations needed to make a number divisible by 10. We have to multiply it by 2 so that the resulting number will be divisible by 10. Our task is to calculate the minimum number of operations needed
3 min read
JavaScript Program to Find the Factors of a Number
We have given the input number and we need to find all the factors of that number. Factors of the number are nothing but the numbers that divide the original input number evenly with the remainder of 0. Below we have added the example for better understanding: Example:Input: n = 10Output: 1 2 5 10In
3 min read
JavaScript Program to Divide Large Number Represented as String
In this article, we will explore dividing large numbers represented as strings in JavaScript. Dividing a large number represented as a string in JavaScript is a common computational task that involves dividing a numerical line by another numerical value. Table of Content Approach 1: Brute ForceAppra
4 min read
JavaScript Program to Find G.C.D Using Recursion
The greatest common divisor (G.C.D) of two integers is the largest positive integer that divides both numbers without leaving a remainder. In JavaScript, finding the greatest common divisor (G.C.D) of two numbers using recursion is a common programming task. Recursion is a powerful technique where a
4 min read
JavaScript Program for Sieve of Eratosthenes
In this article, we will be demonstrating a JavaScript program for the Sieve of Eratosthenes algorithm. The Sieve of Eratosthenes algorithm is an efficient way of getting all the prime numbers that exist in the given range e.g., Input: n =10Output: 2 3 5 7 Input: n = 20 Output: 2 3 5 7 11 13 17 19Ap
2 min read