Algorithm


  1. Input:

    • Accept the starting point of the interval (start).
    • Accept the ending point of the interval (end).
  2. Function isPrime(num):

    • If num is less than or equal to 1, return false (not prime).
    • Loop from i = 2 to the square root of num.
      • If num is divisible evenly by i, return false (not prime).
    • If the loop completes, return true (prime).
  3. Function printPrimeNumbers(start, end):

    • Iterate from i = start to end.
      • If isPrime(i) returns true, print i.
  4. Main Program:

    • Initialize a Scanner for user input.
    • Prompt the user to input the starting point (start) of the interval.
    • Prompt the user to input the ending point (end) of the interval.
    • Display a message indicating the range of prime numbers to be found.
    • Call printPrimeNumbers(start, end) to display prime numbers in the specified range.

 

Code Examples

#1 Code Example- Display Prime Numbers Between two Intervals

Code - Java Programming

public class Prime {

    public static void main(String[] args) {

        int low = 20, high = 50;

        while (low  <  high) {
            boolean flag = false;

            for(int i = 2; i  < = low/2; ++i) {
                // condition for nonprime number
                if(low % i == 0) {
                    flag = true;
                    break;
                }
            }

            if (!flag && low != 0 && low != 1)
                System.out.print(low + " ");

            ++low;
        }
    }
}
Copy The Code & Try With Live Editor

Output

x
+
cmd
23 29 31 37 41 43 47
Advertisements

Demonstration


Java Programing Example to Display Prime Numbers Between Two Intervals-DevsEnv