import java.util.Scanner;
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter starting value");
int a=sc.nextInt();
System.out.println("enter ending values");
int b=sc.nextInt();
for (int i = a; i <= b; i++) {
if (isPrime(i)) {
System.out.print(" " + i);
}
}
}
public static boolean isPrime(int b) {
if (count(b) == 2) {
return true;
}
return false;
}
public static int count(int a) {
int count = 0;
for (int i = 1; i <= a; i++) {
if (a % i == 0) {
count++;
}
}
return count;
}
}
Output:
enter starting value
20
enter ending values
30
23 29
public class PrimeNumber {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("enter starting value");
int a=sc.nextInt();
System.out.println("enter ending values");
int b=sc.nextInt();
for (int i = a; i <= b; i++) {
if (isPrime(i)) {
System.out.print(" " + i);
}
}
}
public static boolean isPrime(int b) {
if (count(b) == 2) {
return true;
}
return false;
}
public static int count(int a) {
int count = 0;
for (int i = 1; i <= a; i++) {
if (a % i == 0) {
count++;
}
}
return count;
}
}
Output:
enter starting value
20
enter ending values
30
23 29
0 comments:
Post a Comment