You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// ******************************************************************** Check Prime Number in Different Ways ********************************************************************
2
+
3
+
// 1. Check if a number is prime:
4
+
5
+
// Check if a number is prime using a basic loop (brut Force Method):
6
+
functionisPrime(num){
7
+
8
+
if(num<2){
9
+
returnfalse;
10
+
}
11
+
12
+
for(leti=2;i<num;i++){
13
+
if(num%i===0){
14
+
returnfalse;
15
+
}
16
+
}
17
+
18
+
returntrue;
19
+
}
20
+
console.log("Is the given number prime?",isPrime(7));
21
+
// Output:
22
+
// Is the given number prime? true
23
+
24
+
// OR
25
+
26
+
// Check if a number is prime using a basic loop (brut Force Method):
27
+
constnum=7;
28
+
// Check if the number is prime
29
+
letIsPrime=true;
30
+
for(leti=2;i<num;i++)
31
+
{
32
+
if(num%i===0)
33
+
{
34
+
IsPrime=false;
35
+
break;
36
+
}
37
+
}
38
+
39
+
console.log("Is The Given Number Prime?",IsPrime);
40
+
// Output:
41
+
// Is The Given Number Prime? true
42
+
43
+
// 2. Check if a number is prime Using Arrow Function:
0 commit comments