Print prime numbers from 1 to 100 in javascript

Before reading this code you should know about Prime Numbers

What is Prime Numbers.
The numbers which is divisible by 1 and itself is Prime numbers. Let’s take an example.
3 is prime number because it is only divisible by 1 and 3
4 is not a prime number because it is also divisible by 2

Javascript Code

var flag = 0;
console.log("The prime numbers from 1 to 100 are:")
for(var i = 1;i < 100; i++)
  {
    flag = 0;
    for(var j = 2;j < i; j++)
      {
        if(i % j == 0)
          {
            flag = 1;
            break;
          }
      }
    
    if(flag == 0)
      {
        console.log(i);
      }
  }

The output of the code is

The prime numbers from 1 to 100 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
Facebook
Twitter
LinkedIn
Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

ABOUT ME !!
Johan William

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.

RECENT POSTS

TEST