×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang program to swap two integer numbers

Last Updated : April 20, 2025

Swapping Two Integer Numbers in Golang

In this program, we will swap two integer numbers and print values of swapped variables on the console screen.

Golang Code to Swap Two Integer Numbers

The source code to swap two integer numbers is given below. The given program is compiled and executed successfully.

package main
import "fmt"

func main() {
    // Declare 3 integer type variables
    var num1 int = 10
    var num2 int = 20
    var num3 int = 0

    fmt.Println("Numbers before swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)

    num3 = num1
    num1 = num2
    num2 = num3

    fmt.Println("Numbers after swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)
}

When executed, this program outputs:

Numbers before swapping:
Num1:  10
Num2:  20
Numbers after swapping:
Num1:  20
Num2:  10

Approach 1: Using a Temporary Variable

You can swap two numbers by using a temporary variable to store one of the values while you assign the new value to the other variable.

Example

The following example demonstrates swapping two integers using a temporary variable:

package main
import "fmt"

func main() {
    var num1 int = 10
    var num2 int = 20
    var num3 int = 0

    fmt.Println("Numbers before swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)

    // Swapping using a temporary variable
    num3 = num1
    num1 = num2
    num2 = num3

    fmt.Println("Numbers after swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)
}

The output of the above code is:

Numbers before swapping:
Num1:  10
Num2:  20
Numbers after swapping:
Num1:  20
Num2:  10

Approach 2: Using Multiple Assignment

You can also swap two variables without using a temporary variable using the multiple assignment.

Example

The following example demonstrates swapping two integers without a temporary variable:

package main
import "fmt"

func main() {
    var num1 int = 10
    var num2 int = 20

    fmt.Println("Numbers before swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)

    // Swapping using multiple assignment
    num1, num2 = num2, num1

    fmt.Println("Numbers after swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)
}

The output of the above code is:

Numbers before swapping:
Num1:  10
Num2:  20
Numbers after swapping:
Num1:  20
Num2:  10

Approach 3: Using XOR Bitwise Operator

You can also use an advanced approach to swapping two numbers is by using the XOR bitwise operator.

Example

The following example demonstrates swapping two integers using the XOR bitwise operator:

package main
import "fmt"

func main() {
    var num1 int = 10
    var num2 int = 20

    fmt.Println("Numbers before swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)

    // Swapping using XOR bitwise operator
    num1 = num1 ^ num2
    num2 = num1 ^ num2
    num1 = num1 ^ num2

    fmt.Println("Numbers after swapping:")
    fmt.Println("Num1: ", num1)
    fmt.Println("Num2: ", num2)
}

The output of the above code is:

Numbers before swapping:
Num1:  10
Num2:  20
Numbers after swapping:
Num1:  20
Num2:  10

Golang Basic Programs »



Related Programs

Advertisement
Advertisement


Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.