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

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.