Java Program to print Prime numbers in a given range
Find the Prime Numbers in a Given Interval in Java
Given an integer input the objective is to check whether or not there are any Prime Numbers in the given interval or range. Therefore, we write a code to Find the Prime Numbers in a Given Interval in Java Language.
Example Input : 2 10 Output : 2 3 5 7
Find all the Prime Numbers in a Given Interval in Java
Given two integer inputs for the range or the interval for the search. The objective is to search and find all the Prime Numbers that lay in the given interval or range. To do so we’ll iterate through the numbers and check whether or not they are prime simultaneously. We’ll use loops to check whether the number has any factors other than 1 and the number itself. Here are few methods we’ll use to Find all the Prime Number in a Given Interval in Java Language.
- Method 1: Using inner loop Range as [2, number-1].
- Method 2: Using inner loop Range as [2, number/2].
- Method 3: Using inner loop Range as [2, sqrt(number)].
- Method 4: Using inner loop Range as [3, sqrt(number), 2].
We’ll discuss the above mentioned methods in the sections mentioned below.
Method 1: Using inner loop Range as [2, number-1]
Working
For two integer variables lower and higher we perform the following,
- We run a loop to iterate through the number in the given interval.
- We check if the number is a prime by checking for factors within the range.
Let’s implement the above mentioned logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int lower = 1, upper = 20; for (int i = lower; i <= upper; i++) if (isPrime (i)) System.out.println (i); } static boolean isPrime (int n) { int count = 0; // 0, 1 negative numbers are not prime if (n < 2) return false; // checking the number of divisors b/w 1 and the number n-1 for (int i = 2; i < n; i++) { if (n % i == 0) return false; } // if reached here then must be true return true; } }
Output
2 3 5 7 11 13 17 19
Method 2: Using inner loop Range as [2, number/2]
Working
For a given two integers lower and upper we perform the following,
- We iterate through the numbers using for loop.
- We check whether or not the number has a factor within the range [2, number/2].
Let’s implement the above mentioned logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int lower = 1, upper = 20; for (int i = lower; i <= upper; i++) if (isPrime (i)) System.out.println (i); } static boolean isPrime (int n) { int count = 0; // 0, 1 negative numbers are not prime if (n < 2) return false; // checking the number of divisors b/w 1 and the number n for (int i = 2; i < n / 2; i++) { if (n % i == 0) return false; } // if reached here then must be true return true; } }
Output
2 3 5 7 11 13 17 19
Method 3: Using inner loop Range as [2, sqrt(number)]
Working
For the two given integer variables lower and upper we perform the following,
- We iterate through the numbers using a for loop.
- We check whether or not the number has factors in the interval [2, sqrt(number)].
Let’s implement the above mentioned logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int lower = 1, upper = 20; for (int i = lower; i <= upper; i++) if (isPrime (i)) System.out.println (i); } static boolean isPrime (int n) { int count = 0; // 0, 1 negative numbers are not prime if (n < 2) return false; // A number n is not a prime, if it can be factored into two factors a & b: // n = a * b /*Now a and b can't be both greater than the square root of n, since then the product a * b would be greater than sqrt(n) * sqrt(n) = n. So in any factorization of n, at least one of the factors must be smaller than the square root of n, and if we can't find any factors less than or equal to the square root, n must be a prime. */ for (int i = 2; i < Math.sqrt (n); i++) { if (n % i == 0) return false; } // if reached here then must be true return true; } }
Output
2 3 5 7 11 13 17 19
Method 4: Using inner loop Range as [3, sqrt(number), 2]
Working
For the two given integer variables lower and upper, we perform the following,
- Iterate through the numbers using for loop with the step size as 2.
- Check if the number has any factors in the range [3, sqrt(number)].
Let’s implement the above logic in Java Language.
Java Code
public class Main { public static void main (String[]args) { int lower = 1, upper = 20; for (int i = lower; i <= upper; i++) if (isPrime (i)) System.out.println (i); } static boolean isPrime (int n) { // 0 and 1 are not prime numbers // negative numbers are not prime if (n <= 1) return false; // special case as 2 is the only even number that is prime else if (n == 2) return true; // Check if n is a multiple of 2 thus all these won't be prime else if (n % 2 == 0) return false; // If not, then just check the odds for (int i = 3; i <= Math.sqrt(n); i += 2) { if (n % i == 0) return false; } return true; } }
Output
2 3 4 5 7 11 13 17 19
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
import java.util.Scanner;
public class PrimeOrNot {
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println(“Enter n1 and n2 “);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
for(int i=n1;i<n2;i++) {
int flag = 1;
if(i%2==0)
{
flag = 0;
}
if (i == 0 || i == 1) {
continue;
}
for (int j = 3; j*j < n2; j+=2) {
if (i % j == 0) {
flag = 0;
}
}
if(flag==1)
{
System.out.print(i + " ");
}
}
}
}
//10. Java Program to print Prime numbers in a given range
// https://github.com/niirajkumaar/TOP100CODES/tree/master/Placements/src
import java.util.Scanner;
public class Q10PrimeNoInRange {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print(“Enter Start Position:”);
int start= sc.nextInt();
System.out.print(“Enter End Position:”);
int end= sc.nextInt();
int count=0;
for(int i=start;i<=end;i++){
count=0;
for(int j=1;j<=i;j++){
if(i%j==0){
count++;
}
}
if(count==2){
System.out.print(i+" ");
}
}
}
}
public class Prime_number_given_range {
public static void main(String[] args) {
int count = 0;
for (int i = 1; i <= 20; i++) {
for (int j = 2; j < i; j++) {
count = i % j;
if (count == 0)
break;
}
if (i == 2)
System.out.println(i);
else if (count != 0)
System.out.println(i);
}
}
}
package com.company;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println(“enter the first limit”);
int n1 = input.nextInt();
System.out.println(“enter the second limit”);
int n2 = input.nextInt();
int f=0;
for (int i=n1;i<=n2;i++)
{
f=0;
for(int j=1;j<=i;j++)
{
if(i%j==0)
f++;
}
if(f==2)
System.out.print(i +"\t");
}
}
}
import java.util.*;class main
{
public static void main(String[]args){
int a, b,i,j;
System.out.println(“first nu”);
Scanner s= new Scanner (System.in);
a=s.nextInt();
System.out.println(“second nu”);
b=s.nextInt();
for(i=a; i<=b; i++){
for(j=2; j<=i ; j++){
if(i%j==0){
break;
}
}
if(i==j){
System.out.println(j);
}
}
}
}