Skip to main content

Loop, Conditional, Modulus

Updated 1 min readMarkdown
Part of series GopherLabs
Loop, Conditional, Modulus

Use the modulo operator % and a for loop and if statement to print out all the even numbers from 1 to 100

package main

import (
    "fmt"
)

func main() {
    for i := 1; i <= 100; i++ {
        if i%2 == 0 { // try changing the number to 3, 4, etc.
            fmt.Println(i)
        }
    }
}

playground

13 views

About the author

  • DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

No comments yet. Be the first to comment.

GopherLabs

Part 1 of 50

Ultimate Series of Blogs for All Golang Developer

More from this blog