
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Display Prime Numbers Between Intervals Using Function
In this article, we will understand how to display prime numbers between intervals using function. Prime numbers are special numbers who have only two factors 1 and itself and cannot be divided by any other number.
A number is a prime number if its only factors are 1 and itself. 11 is a prime number. Its factors are 1 and 11 itself. Some examples of prime numbers are 2, 3, 5, 7, 11, 13 and so on. 2 is the only even prime number. All other prime numbers are odd numbers.
Below is a demonstration of the same −
Input
Suppose our input is −
Starting number : 1 Ending number : 75
Output
The desired output would be −
The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
Algorithm
Step 1 - START Step 2 - Declare 2 integer values namely my_high, my_low. Step 3 - Read the required values from the user/ define the values Step 4 - Define a function IsPrime which returns Boolean value. The function takes an integer input and checks if the input is divisible by any of its lower number except 1. Step 5 - If yes, it returns false , else it will return true. Step 6 - Using a for loop, iterate from my_low to my_high, for each number, call the function IsPrime. If true is returned , it is a prime number, store the number Step 7 - Display the result Step 8 - Stop
Example 1
Here, the input is being entered by the user based on a prompt. You can try this example live in ourcoding ground tool .
import java.util.Scanner; public class PrimeNumber { public static void main(String[] args) { int my_high, my_low; System.out.println("Required packages have been imported"); Scanner my_scanner = new Scanner(System.in); System.out.println("A reader object has been defined "); System.out.print("Enter the starting number : "); my_low = my_scanner.nextInt(); System.out.print("Enter an ending Number: "); my_high = my_scanner.nextInt(); System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:"); while (my_low < my_high) { if (IsPrime(my_low)) System.out.print(my_low + " "); ++my_low; } } public static boolean IsPrime(int my_input) { boolean flag = true; for (int i = 2; i <= my_input / 2; ++i) { if (my_input % i == 0) { flag = false; break; } } return flag; } }
Output
Required packages have been imported A reader object has been defined Enter the starting number : 1 Enter the ending number : 75 The prime numbers between the interval 1 and 75 are: 1 2 5 3 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
Example 2
Here, the integer has been previously defined, and its value is accessed and displayed on the console.
public class PrimeNumber { public static void main(String[] args) { int my_high, my_low; my_low = 1; my_high = 75; System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high); System.out.println("The prime numbers between the interval " + my_low + " and " + my_high + " are:"); while (my_low < my_high) { if (IsPrime(my_low)) System.out.print(my_low + " "); ++my_low; } } public static boolean IsPrime(int my_input) { boolean flag = true; for (int i = 2; i <= my_input / 2; ++i) { if (my_input % i == 0) { flag = false; break; } } return flag; } }
Output
The starting and ending numbers are defined as 1 and 75 The prime numbers between the interval 1 and 75 are: 1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
- Related Articles
- Swift program to display prime numbers between intervals using function
- Haskell Program to Display Prime Numbers Between Intervals Using Function
- Java Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Armstrong Numbers Between Intervals Using Function
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C++ Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Haskell Program to Display Prime Numbers Between Two Intervals
- Swift Program to display Armstrong Numbers Between Intervals Using Function
- Haskell Program to Display Armstrong Numbers Between Intervals Using Function
- Golang Program to Display Prime Numbers Between Two Intervals using library functions.
- C program to display the prime numbers in between two intervals
- Java Program to Display Armstrong Number Between Two Intervals
- C program to display all prime numbers between 1 to N using for loop
- Java Program to Display All Prime Numbers from 1 to N
