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
    • Related

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

  • Multiply two numbers in Java using scanner| 5 different ways
  • 5 different ways to Divide two numbers in Java using scanner
  • Learn 8 Ways to Subtract Two Numbers Using Methods in Java
  • 10 ways to subtract two numbers in Java
  • Java Code Examples – Multiply Two Numbers in 5 Easy Ways
  • How to Divide two numbers in Java| 5 different ways

tag

Addition (8) Array (38) C++ language (91) C language (98) c sharp (23) Division (8) Function (29) if else (32) Java language (108) JavaScript (5) loops (138) Multiply (8) Oop (2) patterns (66) PHP (13) Python Language (38) Subtraction (9) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2026 Code for Java c | Powered by SuperbThemes