JavaScript Program to Print Prime numbers up to N

Write a JavaScript program to print Prime numbers up to N.



Source Code
<html>
<head>
    <title>Generate prime numbers upto n</title>
</head>
<body>
<h2>Prime numbers upto given number</h2>
<script>
    var n = parseInt(prompt("Enter upto which u want to print prime numbers", "0"));
    var prime;

    for(i = 2; i <= n; i++)
    {
        prime = true;
        for(j = 2; j <= parseInt(i/2); j++)
        {
            if(i % j == 0)
            {
                prime = false;
                break;
            }
        }
        if(prime)
            document.write(i + "   ");
    }
</script>
</body>
</html>
Output





"Coding Hub - Learn to code" app now available on Google Play Store