From 89badfb278642611edc5ee3d4a77d43b8db2c043 Mon Sep 17 00:00:00 2001 From: Ronn4 <72177151+Ronn4@users.noreply.github.com> Date: Sat, 3 Oct 2020 14:13:56 +0530 Subject: [PATCH] Javascript trial prime no.or not JavaScript Program to Check Prime Number --- Javascript Basic | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Javascript Basic diff --git a/Javascript Basic b/Javascript Basic new file mode 100644 index 0000000..d781808 --- /dev/null +++ b/Javascript Basic @@ -0,0 +1,34 @@ +example: Check Prime Number +// program to check if a number is prime or not + +// take input from the user +const number = parseInt(prompt("Enter a positive number: ")); +let isPrime = true; + +// check if number is equal to 1 +if (number === 1) { + console.log("1 is neither prime nor composite number."); +} + +// check if number is greater than 1 +else if (number > 1) { + + // looping through 2 to number-1 + for (let i = 2; i < number; i++) { + if (number % i == 0) { + isPrime = false; + break; + } + } + + if (isPrime) { + console.log(`${number} is a prime number`); + } else { + console.log(`${number} is a not prime number`); + } +} + +// check if number is less than 1 +else { + console.log("The number is not a prime number."); +}