Go Program to Swap Two Numbers

In this post, we will learn how to write a program to swap two numbers using the Go programming language.

This program asks the user to enter two integers and then stores the values of those two integers in two different variables. After that, we use a third variable to swap the values of the two variables.

Suppose the first integer entered by the user is 7, and the second integer is 91. Then, before swapping, the variable “a” contained the value of 7 and the variable “b” contained the value of “91.”

But after the swapping process, the values will be interchanged. In that case, variable “a” will contain the integer “91,” whereas variable “b” will contain the integer “7.”

Before Swapping:- 
First number =  7
Second number =  91
After Swapping:- 
First number =  91
Second number =  7

So, without further ado, let’s begin this tutorial.

Go Program to Swap Two Numbers

package main

import (
	"fmt"
)

func main() {
	var a, b, c int

	fmt.Print("Enter the first integer: ")
	fmt.Scanln(&a)

	fmt.Print("Enter the second integer: ")
	fmt.Scanln(&b)

	fmt.Println("Before Swapping:- ")
	fmt.Println("First number = ", a)
	fmt.Println("Second number = ", b)

	c = a
	a = b
	b = c

	fmt.Println("After Swapping:- ")
	fmt.Println("First number = ", a)
	fmt.Println("Second number = ", b)
}

Output

Enter the first integer: 23
Enter the second integer: 47
Before Swapping:- 
First number =  23
Second number =  47
After Swapping:- 
First number =  47
Second number =  23

How Does This Program Work?

	var a, b, c int

We have declared three integer data type variables named a, b, and c.

	fmt.Print("Enter the first integer: ")
	fmt.Scanln(&a)

	fmt.Print("Enter the second integer: ")
	fmt.Scanln(&b)

The user is asked to enter two integers. The first integer is stored in a named variable, while the second integer is stored in the b-named variable.

	fmt.Println("Before Swapping:- ")
	fmt.Println("First number = ", a)
	fmt.Println("Second number = ", b)

Now, we are displaying the values stored in the first and second variables before swapping. We can use this data as a reference point to check whether the numbers are really swapped or not.

	c = a
	a = b
	b = c

We assign the value of the first variable “a” to the third variable “c.” Here a = 23 and c = 23.

After that, the value of the second variable “b” is assigned to “a.” Here, a = 47 and b = 47.

Since the value of the “a” variable has been swapped, it’s now time to swap the value of the “b” variable. We assigned the value of the “c” variable to the second variable, “b.” Here, b = 23 and c = 23.

Following the preceding procedure, we obtain a = 47 and b = 23.

	fmt.Println("After Swapping:- ")
	fmt.Println("First number = ", a)
	fmt.Println("Second number = ", b)

Since the two numbers are swapped, we will print the swapped values on the console screen with the help of the fmt.Println() method.

Conclusion

Today you learned how to write a program to swap two numbers using the Go programming language.

In this program, we have talked about only one way to swap two numbers, but there are other ways to swap two numbers too.

We will talk about those methods in some other posts. Till then, if you have any doubts, leave your queries in the comment section.

Leave a Comment

Your email address will not be published. Required fields are marked *