Golang Program to print all integers between a range that aren't divisible by either 2 or 3


Let's assume the range is from 0 to 50. We have to print all the integers that aren't divisible by either 2 or 3.

Steps

  • Use a for loop ranging from 0 to 50.
  • Then, use an if statement to check if the number isn't divisible by both 2 and 3.
  • Print the numbers satisfying the condition.

Explanation

  • The for loop ranges from 0 to 50.
  • The number is divided by 2 and 3.
  • If the remainder ≠ 0, the number isn't divisible by either 2 and 3.
  • The number satisfying the condition is printed.

Example

 Live Demo

package main
import "fmt"
func main(){
   for i:=1; i<=50; i++{
      if !(i % 2 == 0 || i % 3 == 0){
         fmt.Println(i)
      }
   }
}

Output

1
5
7
11
13
17
19
23
25
29
31
35
37
41
43
47
49

Updated on: 31-Jul-2021

302 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements