- 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 find prime numbers in a given range
Examples
- Input num1=3 and num2=8 => Prime numbers are: 3, 5, 7
- Input num1=8 and num2=23 => Prime numbers are: 11, 13, 17, 19, 23
Approach to solve this problem
- Step 1: Define a function that accepts two numbers, num1 and num2, type is int.
- Step 2: Iterate between num1 and num2.
- Step 3: If the number is prime, then print that number, else break.
Program
package main import ( "fmt" "math" ) func printPrimeNumbers(num1, num2 int){ if num1<2 || num2<2{ fmt.Println("Numbers must be greater than 2.") return } for num1 <= num2 { isPrime := true for i:=2; i<=int(math.Sqrt(float64(num1))); i++{ if num1 % i == 0{ isPrime = false break } } if isPrime { fmt.Printf("%d ", num1) } num1++ } fmt.Println() } func main(){ printPrimeNumbers(5, 19) printPrimeNumbers(0, 2) printPrimeNumbers(13, 100) }
Output
5 7 11 13 17 19 Numbers must be greater than 2. 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
- Related Articles
- Write a Golang program to find duplicate elements in a given range
- Golang Program to Print Odd Numbers Within a Given Range
- Program to print prime numbers in a given range using C++ STL
- Golang Program to Print all Numbers in a Range Divisible by a Given Number
- Write a Golang program to check whether a given number is prime number or not
- Write a Golang program to find duplicate elements in a given array
- Python - Find the number of prime numbers within a given range of numbers
- Golang Program to Find the Numbers which are Divisible by 7 and Multiple of 5 in a Given Range
- Print prime numbers in a given range using C++ STL
- Write a program in C++ to remove duplicates from a given array of prime numbers
- C++ Program to Generate Prime Numbers Between a Given Range Using the Sieve of Sundaram
- C++ Program to find Numbers in a Range with Given Digital Root
- Prime numbers in a range - JavaScript
- C++ Program to Implement Wheel Sieve to Generate Prime Numbers Between Given Range
- C++ Program to Implement Segmented Sieve to Generate Prime Numbers Between Given Range

Advertisements