# Write a Golang program to search an element in an array

Go ProgrammingServer Side ProgrammingProgramming

#### Cyber Security : Go from Zero to Hero

Most Popular

60 Lectures 5.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

#### C Programming from scratch- Master C Programming

Best Seller

60 Lectures 8 hours

Definition: A number is that is greater than 2 and divisible only by itself and 1.

Examples: Prime numbers are 2, 3, 5, 7, 11, 13, 113, 119, ..., etc.

## Approach to solve this problem

• Step 1: Find square root of the given number, sq_root = √num
• Step 2: If the given number is divisible by a number that belongs to [2, sq_root], then print “Non Prime Number”
• Step 3: If not divisible by any number, then print “Prime Number”

## Program

Live Demo

package main
import (
"fmt"
"math"
)
if num < 2 {
fmt.Println("Number must be greater than 2.")
return
}
sq_root := int(math.Sqrt(float64(num)))
for i:=2; i<=sq_root; i++{
if num % i == 0 {
fmt.Println("Non Prime Number")
return
}
}
fmt.Println("Prime Number")
return
}

func main(){
}
Number must be greater than 2.
Non Prime Number