Algorithm
-
Input: Accept the number for which factors need to be found.
-
Initialize an empty array to store factors.
javascript
let factors = [];
-
Loop from 1 to the given number.
javascript
for (let i = 1; i <= number; i++) {
-
Check if the current number is a factor.
javascript
if (number % i === 0) {
-
If it is a factor, push it to the array.
javascript
factors.push(i);
-
Close the loop.
javascript
}
-
Output the array of factors.
javascript -
console.log(`Factors of ${number}: ${factors}`);
Code Examples
#1 Code Example- Factors of Positive Number
Code -
Javascript Programming
// program to find the factors of an integer
// take input
const num = prompt('Enter a positive number: ');
console.log(`The factors of ${num} is:`);
// looping through 1 to num
for(let i = 1; i < = num; i++) {
// check if number is a factor
if(num % i == 0) {
console.log(i);
}
}
Copy The Code &
Try With Live Editor
Output
Enter a positive number: 12
The factors of 12 is:
1
2
3
4
6
12
The factors of 12 is:
1
2
3
4
6
12
Demonstration
JavaScript Programing Eaxmple to Find the Factors of a Number-DevsEnv