- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to search an element in an array
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
package main import ( "fmt" "math" ) func checkPrimeNumber(num int) { 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(){ checkPrimeNumber(0) checkPrimeNumber(2) checkPrimeNumber(13) checkPrimeNumber(152) }
Output
Number must be greater than 2. Prime Number Prime Number Non Prime Number
- Related Articles
- Write a Golang program to search an element in a sorted array
- Write a Golang program to find the frequency of an element in an array
- Golang program to search an element in the slice
- Swift Program to Search an Element in an Array
- Write a Golang program to reverse an array
- Write a Golang program to find the frequency of each element in an array
- Golang Program To Append An Element Into An Array
- Java Program to Recursively Linearly Search an Element in an Array
- Write a Golang program to find the element with the minimum value in an array
- Golang Program to search an item into the array using interpolation search
- C++ program to search an element in a sorted rotated array
- Golang program to fill an array with a specific element
- Golang Program To Remove All Occurrences Of An Element In An Array
- C program to search an array element using Pointers.
- Golang Program to Find the Largest Element in an Array

Advertisements