- 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
Golang Program to Print all Numbers in a Range Divisible by a Given Number
Let's assume the lower and upper limit for the range is 2 and 10, respectively, and the given number is 2.
Iterate in the range of 2 to 10 and find modulo of 2 and print them.
Steps
- Define variables for upper and lower limit for the range.
- Print statement for upper and lower limit.
- Take input from the user.
- Define a variable (n) to check the divisibility in a range.
- Take the input from users for n.
- Iterate in the range of lowerLimit and upperLimit.
- Find modulo with n in the range and print them.
Example
package main import "fmt" func main(){ var upperLimit, lowerLimit int fmt.Printf("Enter upper limit for the range: ") fmt.Scanf("%d", &lowerLimit) fmt.Printf("Enter lower limit for the range: ") fmt.Scanf("%d", &upperLimit) var n int fmt.Printf("Enter a number n: ") fmt.Scanf("%d", &n) for i:=lowerLimit; i<=upperLimit; i++{ if i%n == 0 { fmt.Println(i) } } }
Output
Enter lower limit for the range: 2 Enter upper limit for the range: 10 Enter a number n: 2 2 4 6 8 10
Advertisements