JavaScript Find Factors Of Positive Number Example

Oct 23, 2021 . Admin



Hello Friends,

Now let's see example of how to find factors of positive number example. We will find factors of positive number using javascript. This is a short guide on factors of positive number. Let's get started with how to find factors of positive number in javascript.

Here i will give you many example how to find factors of positive number using javascript.

Example : 1
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>JavaScript Find Factors Of Positive Number Example - MyWebtuts.com</title>
    </head>
    <body>
        <h3>JavaScript Find Factors Of Positive Number Example - MyWebtuts.com</h3>
        <script type="text/javascript">
            const factor = prompt('Enter a positive number : ');
            document.write(`The factors of ${factor} is :`);

            for(let i = 1; i <= factor; i++) {
                if(factor % i == 0) {
                    document.writeln(i);
                }
            }
        </script>
    </body>
</html>
Output :
Enter a positive integer :
The factorial of 5 is 120. 
Example : 2
<!DOCTYPE html>
<html>
    <head> 
        <meta charset=utf-8 />
        <title>JavaScript Find Factors Of Positive Number Example - MyWebtuts.com</title>
    </head>
    <body>
        <h3>JavaScript Find Factors Of Positive Number Example - MyWebtuts.com</h3>
        <script type="text/javascript">
            function factors(n)
            {
                var num_fact = [], i;

                for (i = 1; i <= Math.floor(Math.sqrt(n)); i += 1)
                if (n % i === 0)
                {
                    num_fact.push(i);
                    if (n / i !== i)
                    num_fact.push(n / i);
                }
                num_fact.sort(function(x, y)
                {
                    return x - y;
                });  
                return num_fact;
            }
            document.write(factors(15));  
        </script>
    </body>
</html>
Output :
1,3,5,15

It will help you...

#Javascript