Prime Number program in java

In this tutorial we are going to learn how to write a program to check whether a given integer number is prime number or not in Java programming language.

What is Prime Number?

A prime number is a natural number that is greater than 1. It cannot be formed by multiplying two smaller natural numbers. In other words we can also define a prime number as it has only two positive divisors: 1 and itself. Or in simple word you can also say that it can be divided by 1 and number itself.

For example

  1. 2: The smallest prime number.
  2. 3: Another small prime number.
  3. 17: Prime number coz it has only positive divisors 1 and 17.

Read this: What is prime number? Prime number program in C.

How our Java program will work?

Our Java program will take one integer number as input.

Suppose if someone gives an input 5 (As we know 5 is Prime number) then our program will give output “given number is a prime number”.

And if someone gives 8  then output will be “given number is not a prime number”.

Program 1: Prime Number Program in Java

In the below program we are using for loop to iterate through potential divisors from 2 to n/2. If the number is divisible by other than 1 and itself, it is not prime. The same we are checking in the program. If number is divisible other that 1 and itself, break the loop and come out.

This approach provides a foundational understanding of prime number determination.

import java.util.*;  
class Main{
    public static void main(String ...a){
    int i=0,temp=0;
    Scanner sc= new Scanner(System.in);
    System.out.print("Enter number- ");  
    int n= sc.nextInt();  
    for(i=2;i<=(n/2);i++){
		if(n%i==0)
		{
		temp=1;
		break;
		}
	}
	if(temp==1)
	    System.out.println("Number is not a prime");
	else
		System.out.println("Number is prime");
	}
}

Output:

Enter number- 11
Number is prime

Program 2: Prime Number Program using Java 8

In the below program we are using the Java 8 streams. First we are converting int input to stream using IntStream and the range() method is to iterate between 2 to Math.sqrt(number) + 1. And using noneMatch method we are checking divisor. If it divides then return false otherwise return true.

import java.util.stream.IntStream;
public class PrimeNumberJava8 {
    public static void main(String ...a){
        int number=31;
        Boolean isPrime = IntStream.range(2, (int) Math.sqrt(number) + 1)
                .noneMatch(i -> number % i == 0);
        if(isPrime)
            System.out.println("Prime Number");
        else
            System.out.println("Not Prime Number");
    }
}

Output

Prime Number

Conclusion

In this tutorial, we have seen writing prime numbers program using for loop and java 8. Our Java programs to determine whether a given integer is a prime number or not.

Understanding Prime Numbers:

  • A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. It has only two positive divisors: 1 and itself.
  • Examples such as 2, 3, and 17 are the prime numbers.

Working of Java Programs:

  1. Traditional Approach:
    • We developed a program using a traditional for loop to iterate through potential divisors from 2 to n/2. If the number has a divisor other than 1 and itself, it is not prime.
    • This approach provides a foundational understanding of prime number determination.
  2. Java 8 Stream Approach:
    • In this approach we have take the power of Java 8 streams, the second program uses IntStream to efficiently check for divisors using the noneMatch method.

Execution and Output:

  • The programs took integer input and provided clear outputs, indicating whether the given number is prime or not.
  • Examples with inputs like 11 and 31 illustrated the correct identification of prime numbers.

Happy coding! 🚀

Leave a Comment