Golang
Golang

 

Introduction

A prime number is a number greater than 1 that has no positive divisors other than 1 and itself.
In other words, a prime number cannot be divided exactly by any other numbers except for 1 and itself.
For example, 2, 3, 5, 7, 11, and 13 are prime numbers, while 4, 6, 8, 9, and 10 are not.

The objective of this program is to check if a given number is a prime number using the Go programming language.
The program will take an integer input from the user and determine if it is prime or not by applying the basic properties of prime numbers.

Code Implementation


package main

import (
    "fmt"
    "math"
)

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

func main() {
    var number int

    // Ask the user for input
    fmt.Print("Enter a number: ")
    fmt.Scan(&number)

    // Check if the number is prime
    if isPrime(number) {
        fmt.Println(number, "is a prime number.")
    } else {
        fmt.Println(number, "is not a prime number.")
    }
}

Explanation of the Program

The program is divided into several sections:

  • isPrime function: This function takes an integer n as input and returns true if the number is prime and false otherwise.
    The function first checks if n is less than or equal to 1. If so, it immediately returns false. Then, it iterates from 2 to the square root of n and checks if n is divisible by any number in that range. If it finds a divisor, it returns false. If no divisors are found, the number is prime, and the function returns true.
  • main function: In the main function, the program asks the user to input an integer, then calls the isPrime function to check if the number is prime.
    Based on the result, it prints whether the number is prime or not.

How to Run the Program

Follow the steps below to run the program:

  1. Ensure you have Go installed on your system. You can download it from Go Downloads.
  2. Create a new file named prime_checker.go and paste the above code into the file.
  3. Open a terminal and navigate to the directory where the file is saved.
  4. Run the program using the command: go run prime_checker.go
  5. The program will prompt you to enter a number. Enter any integer to check if it is prime.

After running the program, it will display whether the entered number is prime or not based on the logic implemented in the code.

© 2024 Go Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)