Home » Programming » Java Interview Programs » Java programs for prime numbers using different techniques

Java programs for prime numbers using different techniques

In this article let us see about the prime numbers, the properties of prime numbers, the different ways to find a number is a prime number or not in java, and the different ways to print the prime numbers in between the two numbers in java.

Before going into the program let us see something about prime numbers.

What is a prime number?

A prime number is a natural number that is greater than 1 and it has exactly two factors 1 and itself. (or) A prime number is a natural number that is greater than 1 and it is divisible by 1 and itself but not other numbers.

For example,

5 is a prime number. It has exactly two factors 1 and 5.

16 is not a prime number. It has more than two factors like 1, 2, 4, 8, and 16. If any natural number that has more than two factors is not a prime number.

The below image displays the prime numbers within 100 starting from 2.

prime-numbers-within100

Some interesting facts about prime numbers

  • 1 is not a prime number because it has only one factor that is itself.
  • A prime number is a natural number that is greater than 1. So obviously 1 and negative numbers are not prime numbers.
  • The only even prime number is 2, and all other even numbers are not prime numbers. (eg : 4, 6, 8, …etc is not a prime number).
  • No prime number that ends with 5, except 5. (eg: 5 is a prime, and 15, 25, 35, …etc is not a prime).
  • We can write any prime number is in the form of 6n+1 (or) 6n-1.

Now let's test any prime number is in the form of 6n+1 or 6n-1. let's take two prime numbers 11 and 97. We can get 11 when we apply n=2 in the formula 6n-1 => 6(2)-1 = 11. We can get 97 when we apply n=16 in the formula 6n+1 => 6(16)+1 = 97.

Note: We can write all prime numbers are in the form of 6n+1 (or) 6n-1. But all the numbers are in the form of 6n+1 (or) 6n-1 doesn't be the prime number. For example, let's take the number 169. We can get 169 when we apply n=28 in the formula 6n+1 => 6(28)+1 = 169. But 169 is not a prime number.

Now let us see the java programs for prime numbers.

Java program to check if a number is a prime number or not using for loop

public class isPrimeNumber {		
//using for loop to check if a number is a prime number or not
public static boolean checkIfPrime(int n){		
	boolean isPrime = true;		
	if(n<=1)
		isPrime = false;
	else{
	        for(int i=2;i<=n/2;i++){
			if(n%i==0&&n!=i){
				isPrime = false;						
				break;
			}				
		}
	}		
	return isPrime;		
}	
public static void main(String[] args) {
	int n = 11;
	if(checkIfPrime(n))
		System.out.println(n+" is a prime number");
	else			
		System.out.println(n+" is not a prime number");		
}
}

Output

if n=11 then the output is 11 is a prime number. if n=22 then the output is 22 is not a prime number.

Now let me explain how the above program works.

We all know that the modulo (%) operator returns the remainder of two numbers after division right. Here we are using the same to find the factors of a number.

  1. First, we have declared the boolean variable isPrime and that is initialized with the value true (isPrime = true).
  2. Next, within the for loop, we are checking if the number has factors other than 1 and itself. If it has other factors then we are making the boolean variable isPrime as false and exit the loop using the break statement.
  3. If the number doesn't have other factors except 1 and itself then the boolean variable isPrime doesn't become false.
  4. Finally, the function returns true if the number has exactly two factors 1 and itself. It returns false if the number has more than two factors.

Java program to check if a number is a prime number or not using while loop

public class isPrimeNumber {		
//using while loop to check if a number is a prime number or not
public static boolean checkIfPrime(int n){		
	boolean isPrime = true;
	int i=2;			
	if(n<=1)
		isPrime = false;
	else{			
		while(i<=n/2){
			if(n%i==0&&n!=i){
				isPrime = false;						
				break;
			}					
			i++;
		}
	}		
	return isPrime;		
}
public static void main(String[] args) {
	int n = 241;
	if(checkIfPrime(n))
		System.out.println(n+" is a prime number");
	else			
		System.out.println(n+" is not a prime number");		
	}
}

Output

if n=241 then the output is 241 is a prime number. if n=87 then the output is 87 is not a prime number.

The above java program uses a While loop and the previous java program uses For loop has the same logic. If you need an explanation of logic then please refer to the same.

Java program to print the prime numbers in between the two numbers using for loop

public class findPrimeNumbersWithRange {	
//using for loop to print the prime numbers in between the two numbers
public static void printPrimeWithinRange(int start, int end){
	boolean isPrime;
	for(int i=start;i<=end;i++){
		isPrime = true;
		if(i<=1)
			isPrime = false;
		else{
			for(int j=2;j<=i/2;j++){
				if(i%j==0&&i!=j){
					isPrime = false;					
					break;
				}
			}
		}
		if(isPrime)
			System.out.print(i+" ");
	}		
}
public static void main(String[] args) {
	int start = 1, end = 50;
	printPrimeWithinRange(start, end);
}
}

Output

if start=1 & end=50 then the output is 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47.

if start=10 & end=40 then the output is 11 13 17 19 23 29 31 37.

Here the same technique is used as the program that uses for loop to check the number is a prime number or not. The only difference is here we have used one extra for loop outside the same logic to print the prime numbers in between the numbers.

Java program to print the prime numbers in between the two numbers using while loop

public class findPrimeNumbersWithRange {	
//using while loop to print the prime numbers in between the two numbers
public static void printPrimeWithinRange(int start, int end){
	boolean isPrime;
	int i=start,j;
	while(i<=end){
		isPrime = true;
		j=2;
		if(i<=1)
			isPrime = false;
		else{
			while(j<=i/2){
				if(i%j==0&&i!=j){
					isPrime = false;					
					break;
				}
				j++;					
			}
		}
		if(isPrime)
			System.out.print(i+" ");
		i++;
	}		
}
public static void main(String[] args) {
	int start = 1, end = 70;
	printPrimeWithinRange(start, end);
}
}

Output

if start=1 & end=70 then the output is 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67.

if start=60 & end=80 then the output is 61 67 71 73 79.

Here the same technique is used as the program that uses a while loop to check a number is a prime number or not. The only difference is here we have used one extra while loop outside the same logic to print the prime numbers in between the numbers.

I hope you understood the prime numbers, how to write a java program to check if a number is a prime number or not, and how to write the java program to print the prime numbers in between the two numbers. Thanks!. Keep Reading!.

Leave a Reply

Your email address will not be published. Required fields are marked *