Golang Program to Print all Numbers in a Range Divisible by a Given Number

Go ProgrammingServer Side ProgrammingProgramming

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

 Live Demo

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
raja
Published on 31-Jul-2021 14:36:56
Advertisements