Algorithm


  1. Create a Function to Check Prime Numbers:

    • Function: isPrime(num)
      • Input: num (an integer)
      • Output: boolean (true if num is prime, false otherwise)
      • Algorithm:
        • If num is less than or equal to 1, return false.
        • Iterate from i = 2 to the square root of num.
          • If num is divisible evenly by i, return false.
        • If no divisor is found, return true.
  2. Create a Function to Display Prime Numbers in a Range:

    • Function: displayPrimeNumbers(start, end)
      • Input: start and end (integers representing the interval)
      • Output: None (prints prime numbers in the specified range)
      • Algorithm:
        • Print "Prime numbers between start and end are:"
        • Iterate from i = start to end.
          • If isPrime(i) is true, print i.
  3. Main Method:

    • Input: None
    • Output: None
    • Algorithm:
      • Set intervalStart to the starting value of the interval.
      • Set intervalEnd to the ending value of the interval.
      • Call displayPrimeNumbers(intervalStart, intervalEnd).

 

Code Examples

#1 Code Example- Prime Numbers Between Two Integers

Code - Java Programming

public class Prime {

    public static void main(String[] args) {

        int low = 20, high = 50;

        while (low  <  high) {
            if(checkPrimeNumber(low))
                System.out.print(low + " ");

            ++low;
        }
    }

    public static boolean checkPrimeNumber(int num) {
        boolean flag = true;

        for(int i = 2; i <= num/2; ++i) {

            if(num % i == 0> {
                flag = false;
                break;
            }
        }

        return flag;
    }
}
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 Intervals Using Function-DevsEnv