Go Program to Print Odd Numbers

The for loop in this Go Program to Print Odd Numbers from 1 to n starts at one and stops at oddnum. Inside it, the if condition (x % 2 != 0) checks whether the remainder of the number divisible by two does not equal zero. If True, it prints that odd number.

package main

import "fmt"

func main() {

    var odnum int

    fmt.Print("Enter the Number to Print Odd's = ")
    fmt.Scanln(&odnum)

    for x := 1; x <= odnum; x++ {
        if x%2 != 0 {
            fmt.Print(x, "\t")
        }
    }
}
Enter the Number to Print Odd's = 10
1       3       5       7       9 

Golang Program to Print Odd Numbers from 1 to N

In this Golang program, for loop starts at one and increases by two (x = x + 2). It indicates all the numbers will be odd, and no need to add extra if condition to check the remainder. 

package main

import "fmt"

func main() {

    var odnum int

    fmt.Print("Enter the Number to Print Odd's = ")
    fmt.Scanln(&odnum)

    for x := 1; x <= odnum; x = x + 2 {
        fmt.Print(x, "\t")
    }
}
Enter the Number to Print Odd's = 20
1       3       5       7       9       11      13      15      17      19

This Go program displays the odd numbers from minimum to maximum. The first if statement (Oddmin % 2 == 0) checks whether the min value is even and if it is true, the minimum value increments by one (oddmin++) to become an odd number. Within the for loop, we incremented the odd value by two so that all the numbers will be odd.

package main

import "fmt"

func main() {

    var oddmin, oddmax int

    fmt.Print("Enter the Minimum to Start Printing Odd's = ")
    fmt.Scanln(&oddmin)
    fmt.Print("Enter the Maximum to End Printing Odd's = ")
    fmt.Scanln(&oddmax)

    if oddmin%2 == 0 {
        oddmin++
    }
    fmt.Print("The Odd Numbers from ", oddmin, " to ", oddmax, " are \n")
    for i := oddmin; i <= oddmax; i = i + 2 {
        fmt.Print(i, "\t")
    }
}
Enter the Minimum to Start Printing Odd's = 10
Enter the Maximum to End Printing Odd's = 30
The Odd Numbers from 11 to 30 are 
11      13      15      17      19      21      23      25      27      29

This Golang odd numbers example is the same as the first one. However, it prints the odd numbers that start at minimum and end at the maximum limit.

package main

import "fmt"

func main() {

    var oddmin, oddmax int

    fmt.Print("Enter the Minimum to Start Printing Odd's = ")
    fmt.Scanln(&oddmin)
    fmt.Print("Enter the Maximum to End Printing Odd's = ")
    fmt.Scanln(&oddmax)

    fmt.Print("The Odd Numbers from ", oddmin, " to ", oddmax, " are \n")
    for i := oddmin; i <= oddmax; i++ {
        if i%2 != 0 {
            fmt.Print(i, "\t")
        }
    }
}
Golang Program to Print Odd Numbers 4