Java Program to Display Prime Numbers Between Two Intervals

In this Java program, we will learn how to Display Prime Numbers Between Two Intervals using Java.  

How to Display Prime Numbers Between Two Intervals? 

Example 1:  Display prime numbers between two intervals.

RUN CODE SNIPPET
public class Main { 
    public static void main(String[] args) { 
        int low = 100, high = 500; 
        while (low < high) { 
            boolean flag = false; 
            for(int i = 2; i <= low/2; ++i) { 
                if(low % i == 0) { 
                    flag = true; 
                    break; 
                } 
            } 
            if (!flag && low != 0 && low != 1) 
                System.out.print(low + " "); 
            ++low; 
        } 
    } 
}

OUTPUT 

101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499

In the above program, low and high are tested for prime between each number. 

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Java is a popular programming language that is used to develop a wide range of applications. If you are a...
  • Java
  • March 6, 2023
Java is a programming language and computing platform that is used to create various types of applications. It is used...
  • Java
  • March 6, 2023
In this post, you’ll learn how to download and install JUnit in Eclipse so that you can use in your...
  • Java
  • October 9, 2022