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

In this Java program, we will learn how to find the GCD of two numbers using Java.  GCD (Greatest Common...
  • Java
  • December 3, 2024
In this Java Program, you’ll learn how to swap two numbers using the Java programming language.  How to Swap Two...
  • Java
  • December 2, 2024
In this Java program , we will learn how to Find Largest Element in an Array in your Java program.   How to Find Largest Element...
  • Java
  • December 2, 2024