Program to check whether the number is prime or not:
package quipoin.javaeasyprograms;
public class PrimeORNot {
static boolean checkPrimeOrNot(int n) {
int count = 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0)
count++;
}
if (count == 0)
return true;
else
return false;
}
public static void main(String[] args) {
System.out.println(checkPrimeOrNot(19));
}
}
Output:
true
Program to find the nth Prime Number of the series:
package quipoin.javaeasyprograms;
public class NthPrimeNumber {
static int printNthPrime(int x){
int count,j=2,p=1,n=0;
while(p<=x) {
count=0;
for (int i = 2; i <= j / 2; i++) {
if (j % i == 0) {
count++;
break;
}
}
if(count==0) {
n=j;
p++;
}
j++;
}
return n;
}
public static void main(String[] args) {
System.out.println(printNthPrime(5));
}
}
Output:
11
Program to find the number between two numbers:
package quipoin.javaeasyprograms;
import java.util.ArrayList;
import java.util.List;
public class PrimeBetweenTwoNumber {
static List<Integer> findPrimeBetnTwo(int x, int y) {
int count;
List<Integer> ls= new ArrayList<>();
do{
count = 0;
for (int j = 2; j <= x / 2; j++) {
if (x % j == 0) {
count++;
break;
}
}
if (count == 0) {
ls.add(x);
}
x++;
}while (x <= y);
return ls;
}
public static void main(String[] args) {
System.out.println(findPrimeBetnTwo(5,12));
}
}
Output:
[5, 7, 11]