- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write a Golang program to swap two numbers without using a third variable
Approach to solve this problem
- Step 1: Define a function that accepts two numbers, type is int.
- Step 2: Find b = a + b;
- Step 3: Then a = b – a and b = b – a
Program
package main import "fmt" func swap(a, b int){ fmt.Printf("Before swapping, numbers are %d and %d\n", a, b) b = a + b a = b - a b = b - a fmt.Printf("After swapping, numbers are %d and %d\n", a, b) } func main(){ swap(23, 45) swap(56, 100) }
Output
Before swapping, numbers are 23 and 45 After swapping, numbers are 45 and 23 Before swapping, numbers are 56 and 100 After swapping, numbers are 100 and 56
- Related Articles
- How to swap two numbers without using the third or a temporary variable using C Programming?
- How to swap two String variables without third variable.
- How can I swap two strings without using a third variable in Java?
- How to swap two numbers without using a temp variable in C#
- Swap two Strings without using third user defined variable in Java
- Swap Numbers without using temporary variable in Swift Program?
- Swap two Strings without using temp variable in C#
- Swapping two variable value without using third variable in C/C++
- How to swap two arrays without using temporary variable in C language?
- How to Swap Two Numbers in Golang?
- Java program to swap two numbers using XOR operator
- C++ Program to Swap Two Numbers
- Java Program to Swap Two Numbers.
- Haskell Program to Swap Two Numbers
- Kotlin Program to Swap Two Numbers

Advertisements