import java.util.Scanner;
public class Prime {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int no=sc.nextInt();
for (int i = 2;i <= no;i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int num) {
if (num == 2 || num == 3) {
return true;
}
if (num % 2 == 0|| num % 3 == 0) {
return false;
}
for (int i = 3;i < Math.sqrt(num);i =i+ 2) {
if (num % i == 0 || num % Math.sqrt(num) == 0) {
return false;
}
}
return true;
}
}
public class Prime {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number:");
int no=sc.nextInt();
for (int i = 2;i <= no;i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int num) {
if (num == 2 || num == 3) {
return true;
}
if (num % 2 == 0|| num % 3 == 0) {
return false;
}
for (int i = 3;i < Math.sqrt(num);i =i+ 2) {
if (num % i == 0 || num % Math.sqrt(num) == 0) {
return false;
}
}
return true;
}
}
No comments:
Post a Comment