print prime numbers in java code example
Example 1: java prime numbers
public class Main {
public static void main(String[] args) {
for (int i = 2; i < 1_000_000; ++i) {
boolean isPrime = true;
int sqrt = (int)Math.ceil(Math.sqrt(i));
for (int divisor = 2; divisor <= sqrt; ++divisor) {
if (i % divisor == 0) {
isPrime = false;
break;
}
}
if (isPrime) {
System.out.println(i);
}
}
}
}
Example 2: prime number program in java
public class Prime {
public static void main(String[] args) {
int num = 29;
boolean flag = false;
for(int i = 2; i <= num/2; ++i){
if(num % i == 0){
flag = true;
break;
}
}
if (!flag)
System.out.println(num + " is a prime number.");
else
System.out.println(num + " is not a prime number.");
}}
Example 3: Java program to print prime numbers in a given range
// Java program to print prime numbers in a given range
public class PrimeNumberRangeDemo
{
public static void main(String[] args)
{
int lowerRange = 10, higherRange = 50;
while(lowerRange < higherRange)
{
boolean flag = false;
for(int a = 2; a <= lowerRange / 2; ++a)
{
if(lowerRange % a == 0)
{
flag = true;
break;
}
}
if(!flag && lowerRange != 0 && lowerRange != 1)
System.out.print(lowerRange + " ");
++lowerRange;
}
}
}
Example 4: Java program to print prime numbers upto n
// Java program to print prime numbers upto n
import java.util.Scanner;
public class PrimeNumberUptoN
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int a = 0;
int n = 0;
String strPrime = "";
System.out.println("Please enter the value of n: ");
int number = sc.nextInt();
for(a = 1; a <= number; a++)
{
int counter = 0;
for(n = a; n >= 1; n--)
{
if(a % n == 0)
{
counter = counter + 1;
}
}
if(counter == 2)
{
strPrime = strPrime + a + " ";
}
}
System.out.println("prime numbers upto n are: ");
System.out.println(strPrime);
sc.close();
}
}
Example 5: print prime numbers in java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class PrimeNumberChecker3 {
public static List<Integer> generatePrimeInRange(int startRange, int endRange) {
if (startRange >= endRange) {
System.out.println("endRange must be greater then startRange");
System.exit(0);
}
List<Integer> primeList = new ArrayList<>();
while (startRange <= endRange) {
int i = 2, count = 0;
while (i <= startRange / 2) {
if (startRange % i == 0) {
count++;
break;
}
i++;
}
if (count == 0) {
primeList.add(startRange);
}
startRange++;
}
return primeList;
}
public static void main(String[] args) {
System.out.println("Enter a number range to generate prime numbers in between");
Scanner scanner = new Scanner(System.in);
int number1 = scanner.nextInt();
int number2 = scanner.nextInt();
List<Integer> primeNumbers = generatePrimeInRange(number1, number2);
System.out.println("Prime Numbers are");
System.out.println(primeNumbers);
}
}
Example 6: print prime numbers in java
import java.util.Scanner;
public class PrimeNumberChecker {
public static void main(String[] args) {
System.out.println("Enter a number to check Prime or Not");
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();
int i = 2, count = 0;
while (i <= number / 2) {
if (number % i == 0) {
count++;
break;
}
i++;
}
if (count == 0) {
System.out.println(number + " is prime number");
} else {
System.out.println(number + " is not a prime number");
}
}
}