JavaScript Program to check the square root of a number is prime or not

In this program, You will learn how to check the square root of a number is prime or not in JavaScript.


while (Condition) {
    Statement
    Increment/Decrement
}

JavaScript Program to check the square root of a number is prime or not

Example: How to check the square root of a number is prime or not in JavaScript

let n = parseInt(prompt("Enter a number:"));

sq = Math.sqrt(n);
console.log("Square root is:", sq)

num = parseInt(sq);
console.log("Now square root number in integer is:", num)

let s = 0;

let p = 1;
let i = 2;

while (i < num) {
  if (parseInt(num % i) == 0) {
    p = 0;
    break;
  }
  i++;
}
if (p == 1) {
  console.log("Number is prime:" + num);
} else {
  console.log("Number is not prime:" + num);
}

Output:

Enter a number:> 49
Square root is: 7
Now square root number in integer is: 7
Number is prime:7