Golang Program to Read Three Digits and Print all Possible Combinations from the Digits

Go ProgrammingServer Side ProgrammingProgramming

Numbers are: a, b and c => 1, 2, 3

Combination of (a, b, c) are: (1, 1, 1), (1, 2, 1), (1, 2, 2), (1, 2, 3), . . ., (3, 3, 3).

Steps

  • Define the variables, a, b and c.
  • Print statement for the first number and scan the number.
  • Print statement for the second number and scan the number.
  • Print statement for the third number and scan the number.
  • Initialize an array with numbers, a, b and c.
  • Iterate the array with iterator i, j and k.
  • Print (i, j, k)th index number of the array.

Example

 Live Demo

package main
import "fmt"
func main(){
   var a, b, c int
   fmt.Print("Enter first number: ")
   fmt.Scanf("%d", &a)
   fmt.Print("Enter second number: ")
   fmt.Scanf("%d", &b)
   fmt.Print("Enter third number: ")
   fmt.Scanf("%d", &c)
   arr := [3]int{a, b, c}
   for i := range arr{
      for j := range arr{
         for k := range arr{
            fmt.Println(arr[i], arr[j], arr[k])
         }
      }
   }
}

Output

1 1 1
1 1 2
1 1 3
1 2 1
1 2 2
1 2 3
1 3 1
1 3 2
1 3 3
2 1 1
2 1 2
2 1 3
2 2 1
2 2 2
2 2 3
2 3 1
2 3 2
2 3 3
3 1 1
3 1 2
3 1 3
3 2 1
3 2 2
3 2 3
3 3 1
3 3 2
3 3 3
raja
Published on 31-Jul-2021 14:48:34
Advertisements