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
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Declare Variables: Declare variables to store the number and a flag to indicate if the number is prime.
- Input the Number: Use fmt.Scanlnto take input from the user for the number.
- 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.
- Display the Result: Use fmt.Printlnto 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 numberis 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. Thefmt.Scanlnfunction reads the input and stores it in thenumbervariable.
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 forloop 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 anyi, it is not prime, and the loop breaks.
Step 5: Display the Result
- The program uses the isPrimeflag 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.