- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Golang Program to Display Prime Numbers Between Two Intervals using library functions.

In this article we will discuss about how to display prime numbers between two intervals using functions in Go language.

### Syntax

func Sieve(n int) []int func IsPrime(n int) bool If else conditionals in GO language: If condition { // code to be executed } else { // code to be executed } For loop as a while loop in GO language: for condition { // code to be executed // increment or decrement the count variable. }

**Sieve()** function defined in primes package accepts an integer value as an argument and returns an array of integers containing the prime numbers upto that integer value.

**IsPrime()** function defined in primes package accepts a 64 bit unsigned integer value as argument and returns Boolean expression based on whether the number present is prime or not.

There are many methods using which we can display prime numbers between two intervals. We shall discuss these methods in upcoming examples.

## Example 1: Golang program to display Prime Numbers Between Two Intervals using Functions

### Algorithm

**Step 1**− Import the package fmt and primes.**Step 2**− Initialize and define primeNumber() function.**Step 3**− start the main() function.**Step 4**− Initialize the variables of data type int and assign value to it.**Step 5**− call the primeNumber() function and pass the assigned number as command line argument to it.**Step 6**− Print the results on the screen.

### Example

// golang program to display prime numbers using library function. package main import ( "fmt" "github.com/fxtlabs/primes" ) // fmt package allows us to print anything on the screen. // primes package allows us to use methods that help us to use methods related to primes. // initializing and defining the primeNumber() function func primeNumber(number int) []int { // getting the prime numbers in the said range prime := primes.Sieve(number) // returning the result return prime } func main() { // initializing the variables to get the intervals. var number int // printing prime numbers between 2 and 50 number = 50 // calling the primeNumber() function result := primeNumber(number) // printing the results on the screen fmt.Printf("prime numbers between 2 and %d are: ", number) fmt.Println(result) } main.go:5:4: cannot find package "github.com/fxtlabs/primes" in any of: /usr/lib/golang/src/github.com/fxtlabs/primes (from $GOROOT) /home/cg/root/99493/go/src/github.com/fxtlabs/primes (from $GOPATH)

### Output

prime numbers between 2 and 50 are: [2 3 5 7 11 13 17 19 23 29 31 37 41 43 47]

### Description of the Code

First, we import the

*fmt*package that allows us to print anything and primes package that allows us to use other predefined prime methods.Then create and define the

**primeNumber()**function. This function contains the logic to print the prime numbers.Start the

**main()**function.Initialize and define the variable number that will be the upper limit within which prime numbers are to be printed.

Then call the

**primeNumber()**function and pass this value to it as command line argument.Call the

**sieve()**method defined in primes package and pass the number chosen as an argument to it.Store the array of integers returned by this function in a separate variable and return it.

Return this array from

**primeNumbers()**function.Store the results and print them on the screen using

**fmt.Println()**function.

## Example 2: Golang Program to display Prime Numbers Between Two Custom Intervals using Functions.

Let us use another approach to print prime numbers in two intervals using functions.

### Algorithm

**Step 1**− Import the package fmt.**Step 2**− Initialize and define**primeNumber()**function.**Step 3**− start the**main()**function.**Step 4**− Initialize the variables of data type int and assign values to them. These values will form the intervals within which prime numbers are to be calculated.**Step 5**− call the**primeNumber()**function.**Step 6**− Print the results on the screen.

### Example

// golang program to display prime numbers in two custom intervals using library function. package main import ( "fmt" "github.com/afshin/prime" ) // fmt package allows us to print anything on the screen. // prime package allows us to use inbuilt prime functions // initializing and defining the primeNumber() function func primeNumber(lower_limit, upper_limit uint64) { fmt.Printf("prime numbers between %d and %d are:\n", lower_limit, upper_limit) // printing the result for i := lower_limit; i <= upper_limit; i++ { // getting bool value from isprime() function and using it as condition of if loop. if prime.IsPrime(i) { fmt.Println(i) } } } func main() { // initializing the variables to get the intervals from the user var lower_limit, upper_limit uint64 // let us print prime numbers between 5 and 60. // assigning the value of lower_limit and upper_limit variables. lower_limit = 5 upper_limit = 60 // calling the primeNumber() function primeNumber(lower_limit, upper_limit) }

### Output

prime numbers between 5 and 60 are: 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59

### Description of the Code

First, we import the

*fmt*package that allows us to print anything and primes package that allows us to use other predefined prime methods.Then create and define the

**primeNumber()**function. This function contains the logic to print the prime numbers.Start the

**main()**function.Initialize and assign numbers to the variable that will be the upper limit and lower limit within which prime numbers are to be printed.

Then call the

**primeNumber()**function and pass these intervals as arguments to it.Call the

**isprime()**method defined in primes package. This function accepts a 64 bit unsigned integer value as argument and returns Boolean expression based on whether the number present is prime or not.Check this condition for every values between upper limit and lower limit of the intervals.

Print the values which satisfy the above if condition on the screen using fmt.Printf() function.

## Conclusion

We have successfully compiled and executed the Go language program to display prime numbers between two intervals using function along with examples.

- Related Articles
- C++ Program to Display Prime Numbers Between Two Intervals Using Functions
- C++ Program to Display Prime Numbers Between Two Intervals
- Java Program to Display Prime Numbers Between Two Intervals
- Haskell Program to Display Prime Numbers Between Two Intervals
- Swift program to display prime numbers between two intervals
- Java Program to Display Prime Numbers Between Intervals Using Function
- Haskell Program to Display Prime Numbers Between Intervals Using Function
- Swift program to display prime numbers between intervals using function
- C program to display the prime numbers in between two intervals
- Golang Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Numbers Between Intervals Using Function
- Haskell Program to Display Armstrong Numbers Between Intervals Using Function
- Swift Program to display Armstrong Numbers Between Intervals Using Function
- C++ Program to Display Armstrong Number Between Two Intervals
- Java Program to Display Armstrong Number Between Two Intervals