-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.sks.hkr.java; | ||
|
||
import java.util.Scanner; | ||
|
||
|
||
public class JavaPrimality { | ||
|
||
|
||
private static final Scanner scanner = new Scanner(System.in); | ||
|
||
public static void main(String[] args) { | ||
|
||
/** | ||
* Other approach | ||
BigInteger bigInt=scanner.nextBigInteger(); | ||
System.out.println(bigInt.isProbablePrime(100) ? "prime" : "not prime"); | ||
scanner.close(); | ||
*/ | ||
|
||
|
||
String num = scanner.nextLine(); | ||
int n = Integer.parseInt(num); | ||
boolean flag = false; | ||
for (int i = 2; i <= n / 2; i++) { | ||
if (n % i == 0) { | ||
flag = true; | ||
break; | ||
} | ||
} | ||
if (!flag) | ||
System.out.println("prime"); | ||
else | ||
System.out.println(" is not a prime number."); | ||
scanner.close(); | ||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.sks.hkr.java; | ||
|
||
import java.util.Scanner; | ||
import java.util.regex.Pattern; | ||
|
||
public class PatternSyntaxChecker { | ||
|
||
public static void main(String[] args) { | ||
Scanner in = new Scanner(System.in); | ||
int testCases = Integer.parseInt(in.nextLine()); | ||
while (testCases > 0) { | ||
String pattern = in.nextLine(); | ||
//Write your code | ||
try { | ||
Pattern pat = Pattern.compile(pattern); | ||
System.out.println("Valid"); | ||
} catch (Exception e) { | ||
System.out.println("Invalid"); | ||
} | ||
testCases--; | ||
} | ||
} | ||
|
||
|
||
} |