Go Program to Check if a Number is Prime

Introduction

A prime number is a natural number greater than 1 that is only divisible by 1 and itself. In other words, a prime number has no positive divisors other than 1 and itself. This guide will demonstrate how to write a Go program that checks whether a given number is prime.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a number.
  • Checks whether the number is prime.
  • Displays the result.

Example:

  • Input: 7
  • Output: 7 is a prime number

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Declare Variables: Declare variables to store the number and a flag to indicate if the number is prime.
  4. Input the Number: Use fmt.Scanln to take input from the user for the number.
  5. Check if the Number is Prime: Use a loop and conditional statements to check if the number is divisible by any number other than 1 and itself.
  6. Display the Result: Use fmt.Println to display whether the number is prime.

Go Program

package main

import "fmt"

/**
 * Go Program to Check if a Number is Prime
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare a variable to hold the number
    var number int

    // Step 2: Prompt the user to enter a number
    fmt.Print("Enter a number: ")
    fmt.Scanln(&number)

    // Step 3: Check if the number is less than 2 (not prime)
    if number < 2 {
        fmt.Println(number, "is not a prime number")
    } else {
        isPrime := true

        // Step 4: Check if the number is divisible by any number other than 1 and itself
        for i := 2; i*i <= number; i++ {
            if number%i == 0 {
                isPrime = false
                break
            }
        }

        // Step 5: Display the result based on the flag
        if isPrime {
            fmt.Println(number, "is a prime number")
        } else {
            fmt.Println(number, "is not a prime number")
        }
    }
}

Explanation

Step 1: Declare Variables

  • The variable number is declared as an integer to store the input number.

Step 2: Input the Number

  • The program prompts the user to enter a number using fmt.Print. The fmt.Scanln function reads the input and stores it in the number variable.

Step 3: Check for Numbers Less Than 2

  • Since prime numbers are greater than 1, the program checks if the number is less than 2. If it is, the program immediately prints that the number is not prime.

Step 4: Check Divisibility

  • The program uses a for loop to check if the number is divisible by any number other than 1 and itself. The loop runs from 2 to the square root of the number (i*i <= number) to optimize performance. If the number is divisible by any i, it is not prime, and the loop breaks.

Step 5: Display the Result

  • The program uses the isPrime flag to determine whether the number is prime or not and prints the appropriate message.

Output Example

Example 1:

Enter a number: 7
7 is a prime number

Example 2:

Enter a number: 10
10 is not a prime number

Example 3:

Enter a number: 1
1 is not a prime number

Conclusion

This Go program demonstrates how to check whether a number is prime. It covers basic programming concepts such as loops, conditional statements, and arithmetic operations. This example is useful for beginners learning Go programming and understanding the concept of prime numbers.

Leave a Comment

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

Scroll to Top