;

Java Program to Display the Prime Numbers Between Two Intervals


Tutorialsrack 04/07/2021 Java

In this java program, you’ll learn how to display the prime numbers between two intervals such as 1 to 100 or 1 to nth. In this program, we used the following java basics such as for loop, while loop, and if..else condition.

Here is the program to display the prime number between two intervals.

Code - Java Program to Display the Prime Numbers Between Two Intervals
//Java Program to Display Prime Numbers Between Two Intervals

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {
		int low, high;

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter a Start Number: ");
		low = sc.nextInt();

		System.out.println("Enter an End Number: ");
		high = sc.nextInt();

		System.out.println("All Prime Number Between Interval of " + low + " to " + high + " are:");

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

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

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

			++low;
		}
	}
}
Output

Enter a Start Number: 

20

Enter an End Number: 

80

All Prime Number Between Interval of 20 to 80 is:

23 29 31 37 41 43 47 53 59 61 67 71 73 79 


Related Posts



Comments

Recent Posts
Tags