Golang Program to Print Odd Numbers Within a Given Range

Go ProgrammingServer Side ProgrammingProgramming

To print odd number in a range, we can take two inputs, a and b, for lower and upper limits.

Example

a = 2 and b = 9

Numbers between a and b are: 2, 3, 4, 5, 6, 7, 8, 9

Odd numbers are: 3, 5, 7, 9

Steps

  • Define two numbers, a and b.
  • Take user inputs for the numbers, a and b.
  • Iterate the number between a and b.
  • Find the modulo of 2, print the number if modulo of 2 of that number is not zero.

Example

 Live Demo

package main
import "fmt"
func main(){
   var a, b int
   fmt.Print("Enter lower limit number: ")
   fmt.Scanf("%d", &a)
   fmt.Print("Enter higher limit number: ")
   fmt.Scanf("%d", &b)
   for i := a; i<=b; i++{
      if i%2!=0 {
         fmt.Println(i)
      }
   }
}

Output

Enter lower limit number: 2
Enter higher limit number: 9
3
5
7
9
raja
Published on 31-Jul-2021 14:49:34
Advertisements