Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Java program to Display all prime numbers between two intervals

Java program to Display all prime numbers in an intervals

Posted on November 26, 2020

Table of Contents

  • Java program to Display all prime numbers in an intervals
    • Display prime numbers between two intervals
      • Display prime numbers between two intervals using for loops
      • Display prime numbers between two intervals using while loop
      • Display prime numbers between two intervals using Do-while loop
      • Display prime numbers between two intervals using Method

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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes