java

Java program to Display all prime numbers in an intervals

Java program to Display all prime numbers in an intervals

In this article, we will discuss the concept of Java program to Display all prime numbers in an intervals

In this program, we are going to learn how to write the code to display prime numbers between two intervals using different methods in Java language.

This is done using for loop , while loop , do-while loop and method in Java language

Display prime numbers between two intervals

Display prime numbers between two intervals using for loops

In this program, we will print prime numbers between two intervals using a for loop in Java language

Program 1

import java.util.Scanner;
public class DisplayPrimeTwoIntervals{
public static void main(String args[]){
  int i,count;
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
  System.out.println("Please Enter minimum value : ");
  //Ask input from user
int minValue=sc.nextInt();
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in given range
for(i=minValue; i<=maxValue; i++){

  count=0;
  for(int j=1; j<=i; j++){
    if(i%j==0){
      count++;
    }
}
if(count==2)
  System.out.print(i+" ");
}
}
}

When the above code is executed, it produces the following result

Please enter minimum value:
10
Please enter maximum value:
100
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

 

Display prime numbers between two intervals using while loop

In this program, we will print prime numbers between two intervals using a while loop in Java language

Program 2

import java.util.Scanner;
public class DisplayPrimeTwoIntervalsWhile{
public static void main(String args[]){
  int i,count;
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
  System.out.println("Please Enter minimum value : ");
  //Ask input from user
int minValue=sc.nextInt();
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in given range
i=minValue;
while(i<=maxValue){

  count=0;
  int j=1;
  while(j<=i){
    if(i%j==0){
      count++;
    }
    j++;
}
if(count==2)
  System.out.print(i+" ");
 i++;
}
}
}

When the above code is executed, it produces the following result

Please enter minimum value:
30
Please enter maximum value:
80
31 37 41 43 47 53 59 61 67 71 73 79

 

Display prime numbers between two intervals using Do-while loop

In this program, we will print prime numbers between two intervals using a do- while loop in Java language

Program 3

import java.util.Scanner;
public class DisplayPrimeTwoIntervalsDoWhile{
public static void main(String args[]){
  int i,count;
  Scanner sc=new Scanner(System.in);
  //scanner class for input
  
  System.out.println("Please Enter minimum value : ");
  //Ask input from user
int minValue=sc.nextInt();
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : ");

//forloop for finding and printing all prime numbers in given range
i=minValue;
while(i<=maxValue){

  count=0;
  int j=1;
  while(j<=i){
    if(i%j==0){
      count++;
    }
    j++;
}
if(count==2)
  System.out.print(i+" ");
 i++;
}
}
}

When the above code is executed, it produces the following result

Please enter minimum value:
25
Please enter maximum value:
75
29 31 37 41 43 47 53 59 61 67 71 73


Display prime numbers between two intervals using Method

In this program, we will print prime numbers between two intervals using a method  in Java language

Program 4

import java.util.Scanner;
public class DisplayPrimemethod2{
public static void main(String args[]){
  Scanner sc=new Scanner(System.in);
  
  
  System.out.println("Please Enter minimum value : ");
  //Ask input from user
int minValue=sc.nextInt();
System.out.println("Please Enter maximum value: ");
int maxValue=sc.nextInt();

System.out.println("Prime numbers between"+minValue+" to "+maxValue+" are : ");


while(minValue<maxValue){
  if(printPrime(minValue))
  System.out.print(minValue+" ");
  minValue++;
}

}

public static boolean printPrime(int num)
{
  boolean flag=true;
  for(int i=2; i<=num/2; ++i){
    if(num%i==0){
       flag=false;
         break;
  }

}
return flag;
}
}

When the above code is executed, it produces the following result

Please enter minimum value:
25
Please enter maximum value:
90
29 31 37 41 43 47 53 59 61 67 71 73 79 83 89

 

Suggested for you

if statements in Java language

Nested if in Java language

Data type and variable in Java

Operator in Java language

Method in Java language

For loop in Java language

while loop in Java language

Do-while loop in Java language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.