;

Java Program to Display Prime Numbers Between Intervals Using Function


Tutorialsrack 10/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 using a function. 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 using a function.

Program - Java Program to Display Prime Numbers Between Intervals Using Function
//Java Program to Display Armstrong Numbers Between Intervals Using Function

import java.util.Scanner;

public class JavaPrograms {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

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

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

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

		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;
	}
}
Output

Enter a Start Number: 

1

Enter an End Number: 

100

All Armstrong Number Between Intervals of 1 to 100 are:

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 


Related Posts



Comments

Recent Posts
Tags