JavaScript Program to Find the Factors of a Number
Last Updated :
28 May, 2024
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 = 10
Output: 1 2 5 10
Input: n = 12
Output: 1 2 3 4 6 12
Approach 1: Using For Loop
Here, we are finding the factors of the input number by using the simple and traditional method that is for loop. We are firstly iterating through the values of 1 to the input number (12). Then using the if condition, we are checking whether the n is exactly divisible or not. This is the simplest method to find the factors of the input number.
Example: This example implements the above-approach.
JavaScript
let n = 12;
let i = 1;
for (i = 1; i < n; i++) {
if (n % i == 0) {
console.log(i);
}
}
console.log(n);
Approach 2: Using Spread Operator
Here we are using the Spread operator of JavaScript and the keys method to find the factors of the input number given by the user. The factors are stored in the array and printed using the console.log function.
Example: This example implements the above-approach.
JavaScript
let fact = (n) =>
[...Array(n + 1).keys()]
.filter(
(i) => n % i === 0
);
console.log(fact(12));
Output[ 1, 2, 3, 4, 6, 12 ]
Approach 3: Using the reduce method
Here, we will be using the reduce method to iterate over the elements and find the factors of the input number specific to the variable. This uses the ES6 feature for iteration and stores the output factors in the array. Then we print these factors in the console.
Example: This example implements the above-approach.
JavaScript
let n = 12;
[...Array(n + 1).keys()].reduce(
(_, i) => {
if (i !== 0 && n % i === 0) {
console.log(i);
}
}
);
Approach 4: Using the map method
Here, we are using the map method which creates an array by calling the specific function on each of the elements present in the parent array. Mostly, the map method is used to iterate through the array and call function on every element of the array.
Example: This example implements the above-approach.
JavaScript
let n = 12;
let fact = [...Array(n + 1).keys()]
.map((i) => {
if (n % i === 0) {
return i;
}
})
.filter((i) => i !== undefined);
console.log(fact);
Output[ 1, 2, 3, 4, 6, 12 ]
Approach 5: Using a While Loop
In this approach, we utilize a while loop to find the factors of the input number. We start iterating from 1 and continue until reaching the input number. For each iteration, we check if the input number is divisible by the current iteration value with a remainder of 0. If it is, then the current iteration value is a factor of the input number, and we add it to the list of factors.
Example: This example demonstrates the utilization of a while loop to find the factors of a given input number.
JavaScript
let n = 12;
let i = 1;
let factors = [];
while (i <= n) {
if (n % i === 0) {
factors.push(i);
}
i++;
}
console.log(factors);
Output[ 1, 2, 3, 4, 6, 12 ]
Similar Reads
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 Prime Factors In this article, we will see all the methods to find the prime factors of a number using JavaScript. Methods to Find Prime Factors: Table of Content Using loops and Math.pow() MethodRecursive Approach with Spread OperatorSieve of Eratosthenes AlgorithmMethod 1: Using loops and Math.pow() MethodIn th
3 min read
JavaScript Program to Find Factorial of a Number using Recursion In this article, we are going to learn about finding a Factorial of a Number using Recursion. Calculating the factorial of a number using recursion means implementing a function that calls itself to find the factorial of a given number. The factorial of a non-negative integer "n" is the product of a
2 min read
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 Find All Factors of a Natural Number Finding all factors of a natural number is a common task in programming and mathematics. A factor of a number is an integer that can be multiplied by another integer to produce the original number. In this article, we will discuss various approaches to finding all factors of a natural number using J
2 min read
JavaScript Program to Find LCM of Two Numbers In this article, we are going to learn about finding the LCM of two numbers by using JavaScript. LCM (Least Common Multiple) of two numbers is the smallest positive integer that is divisible by both numbers without leaving a remainder. It's a common multiple of the numbers. LCM of two numbers = Prod
4 min read
Java Program to Find Factorial of a Number Recursively Factorial of a number n is defined as a product of all positive descending integers, Factorial of n is denoted by n!. Factorial can be calculated using the following recursive formula where the recursive call is made to a multiplicity of all the numbers lesser than the number for which the factorial
4 min read
PHP Program to Find all factors of a Number Given a Number, the task is to find all factors of the number in PHP. Factors are numbers that divide another number without leaving a remainder. In this article, we will cover all approaches to find all factors of a number using PHP. Table of Content Using a LoopUsing Square Root OptimizationUsing
3 min read
Java Program to Perform the Unique Factorization of a Given Number Given a number n, the task is to write an efficient program to print all unique prime factors of n. Example Input: 12 Output: 2, 3 Explanation: All prime factors or 12 are 2, 2, 3. In those factors, unique factors are 2 and 3. Input: 315 Output: 3, 5, 7 Steps to find all prime factors 1) While n is
6 min read
PHP Program to Print All Prime Factors of a Given Number Prime factors of a number are the prime numbers that divide the given number exactly. Finding prime factors is a common task in number theory and has applications in cryptography and algorithm design. In this article, we will discuss efficient ways to print all prime factors of a given number in PHP
3 min read