Go Program to Print All Prime Numbers Within a Range

1. Introduction

A prime number is a natural number greater than 1 that cannot be formed by multiplying two smaller natural numbers. In other words, if a number is prime, it has only two distinct positive divisors: 1 and itself. For example, 5 is prime because the only ways to create the number 5 by multiplying integers are 1 × 5 or 5 × 1. In contrast, numbers greater than 1 that have more than two positive divisors are called composite. Notably, 1 is neither prime nor composite.

In this tutorial, we'll construct a Go program to list all prime numbers within a designated range.

2. Program Overview

Our Go program is constructed to:

1. Request the user to stipulate a range (start and end points).

2. Determine all prime numbers within the given range.

3. Exhibit the identified prime numbers to the user.

3. Code Program

// We initiate our Go program by declaring the main package.
package main

// The fmt package is incorporated to oversee input and output proceedings.
import "fmt"

// Function to check if a number is prime.
func isPrime(num int) bool {
    if num <= 1 {
        return false
    }
    for i := 2; i*i <= num; i++ {
        if num % i == 0 {
            return false
        }
    }
    return true
}

// Function to display all prime numbers within a specified range.
func primesInRange(start, end int) []int {
    var primes []int
    for i := start; i <= end; i++ {
        if isPrime(i) {
            primes = append(primes, i)
        }
    }
    return primes
}

// Our program's execution originates from the main function.
func main() {
    var start, end int

    // Users are prompted to provide the range's start and end points.
    fmt.Print("Enter the start of the range: ")
    fmt.Scan(&start)
    fmt.Print("Enter the end of the range: ")
    fmt.Scan(&end)

    // Display the prime numbers located within the provided range.
    fmt.Printf("Prime numbers between %d and %d are: %v\n", start, end, primesInRange(start, end))
}

Output:

Assuming the user defines the range as 10 to 50, the program's output would be:
Prime numbers between 10 and 50 are: [11 13 17 19 23 29 31 37 41 43 47]

4. Step By Step Explanation

1. Package and Import Statements: The expedition begins with the package main assertion, indicating our program's commencement. The fmt package is utilized for I/O operations.

2. Prime Check Function: The isPrime function ascertains if a provided integer is prime. A number less than or equal to 1 is immediately dismissed. The function then verifies if the number has divisors other than 1 and itself.

3. Function to Identify Primes in Range: The primesInRange function traverses each number within the designated range and appends prime numbers to the primes slice.

4. Variable Initialization: Two integer variables, start and end, are initialized to store the range's boundaries.

5. User Input and Display: After gathering the range limits, the program calculates and presents the prime numbers within the said range.

Comments