Skip to content

Commit 8f0f166

Browse files
isPrime added using js
1 parent 25e7135 commit 8f0f166

File tree

4 files changed

+82
-21
lines changed

4 files changed

+82
-21
lines changed
741 Bytes
Binary file not shown.

01-GettingStarted/IsPrimeNumber/Main.java renamed to 01-GettingStarted/03-IsPrimeNumber/Main.java

+26-21
Original file line numberDiff line numberDiff line change
@@ -39,28 +39,33 @@
3939

4040
import java.util.*;
4141

42-
public class Main{
43-
public static void main(String[] args) {
44-
Scanner scn = new Scanner(System.in);
45-
int low = scn.nextInt();
46-
int high = scn.nextInt();
47-
primeTillN(low ,high);
48-
}
42+
public class Main {
43+
44+
public static void main(String[] args){
45+
Scanner scn = new Scanner(System.in);
46+
int t = scn.nextInt() ;
4947

50-
public static void primeTillN(int low , int high){
51-
for(int num = low; num<=high; num++){
52-
boolean ans = isPrime(num);
53-
if(ans == true){
54-
System.out.println(num);
55-
}
56-
}
48+
49+
for(int i=0; i<t; i++){
50+
int n = scn.nextInt();
51+
boolean IsPrimeNumber = isPrime(n);
52+
if(IsPrimeNumber){
53+
System.out.println("prime");
54+
}else{
55+
System.out.println("not prime");
56+
}
57+
}
58+
5759
}
58-
public static boolean isPrime(int num){
59-
for(int i=2; i*i<num; i++){
60-
if(num %2 ==0){
61-
return false;
62-
}
60+
61+
62+
public static boolean isPrime(int number){
63+
for(int i=2; i*i<=number; i++){
64+
if(number%i ==0){
65+
return false;
6366
}
64-
return true;
65-
}
67+
}
68+
return true;
69+
}
70+
6671
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const readline = require("readline");
2+
3+
const rl = readline.createInterface({
4+
input: process.stdin,
5+
output: process.stdout,
6+
});
7+
8+
function main() {
9+
rl.question("", function (input) {
10+
const t = parseInt(input);
11+
if (isNaN(t)) {
12+
console.log("Please enter a valid number");
13+
rl.close();
14+
}
15+
16+
processTestCase(t, 0);
17+
});
18+
}
19+
20+
function processTestCase(totalTest, currTestCase) {
21+
if(totalTest ===currTestCase){
22+
rl.close();
23+
return;
24+
}
25+
26+
rl.question("", function (number) {
27+
let n = parseInt(number);
28+
if(isNaN(n)){
29+
console.log("Please enter a valid number");
30+
rl.close();
31+
}
32+
33+
else {
34+
let isPrime = isPrimeNumber(n);
35+
if(isPrime){
36+
console.log("Number is Prime");
37+
}else {
38+
console.log("number is not prime");
39+
}
40+
}
41+
42+
processTestCase(totalTest , currTestCase +1);
43+
44+
});
45+
}
46+
47+
function isPrimeNumber(num) {
48+
for (let i = 2; i * i <= num; i++) {
49+
if (num % i == 0) {
50+
return false;
51+
}
52+
}
53+
return true;
54+
}
55+
56+
main();
-737 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)