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
- 2: The smallest prime number.
- 3: Another small prime number.
- 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:
- 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.
- 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 thenoneMatch
method.
- In this approach we have take the power of Java 8 streams, the second program uses
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! 🚀
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…