JavaScript Program to Find Prime Factor

You are Here:

What is Prime Factor?

A Prime Factors of a given number is that any prime number other than 1 and itself that exactly divides the given number. For example, the prime factors of 60 is 2, 3, 5

Examples

The following table provides few examples of prime factors of a number.

NumberPrime Factors
555, 11
1002, 5
1862, 3, 31

Find Prime Factor

In the following example, we will find all the Prime factors of the given number (186).

Example

HTML Online Editor
<!DOCTYPE html> <html> <body> <h1>JS Prime Factor</h1> <script> var num = 186; var i, j; var count = 0; var flag = 0; for(i = 2; i < num; i++) { // check for divisibility if(num % i == 0) { count = 0; // check for prime number for(j = 1; j <= i; j++) { if(i % j == 0) count++; } if(count == 2) { if(flag == 0) document.write("Prime factor of "+ num+":<br>"); document.write(i +", "); flag = 1; } } } if(flag == 0) document.write("There is no Prime factor for " +num); </script> </body> </html>

Reminder

Hi Developers, we almost covered 97% of JavaScript Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in JavaScript.