- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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 check whether a given number is prime number or not
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 check whether a given number is a palindrome or not
- C++ Program to Check Whether a Number is Prime or Not
- C Program to Check Whether a Number is Prime or not?
- Java Program to Check Whether a Number is Prime or Not
- How to check whether a number is a prime number or not?
- Write a C# program to check if a number is prime or not
- How To Check Whether a Number is Pointer Prime Number or Not in Java?
- Check whether the given number is Wagstaff prime or not in Python
- How to check whether a number is prime or not using Python?
- Program to check whether given number is Narcissistic number or not in Python
- Program to check whether every rotation of a number is prime or not in Python
- Write a Golang program to check whether a given array is sorted or not (Using Bubble Sort Technique)
- C# Program to check if a number is prime or not
- Python program to check if a number is Prime or not
- PHP program to check if a number is prime or not

Advertisements