Algorithm


  1. Input: Accept the number for which factors need to be found.

  2. Initialize an empty array to store factors.

    javascript
    let factors = [];
     
  3. Loop from 1 to the given number.

    javascript
    for (let i = 1; i <= number; i++) {
     
  4. Check if the current number is a factor.

    javascript
        if (number % i === 0) {
     
  5. If it is a factor, push it to the array.

    javascript
            factors.push(i);
     
  6. Close the loop.

    javascript
        }
     
  7. Output the array of factors.

    javascript
  8. 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

x
+
cmd
Enter a positive number: 12
The factors of 12 is:
1
2
3
4
6
12
Advertisements

Demonstration


JavaScript Programing Eaxmple to Find the Factors of a Number-DevsEnv

Previous
JavaScript Practice Example #3 - Assign 3 Variables and Print Good Way