- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
How to Swap Two Numbers in C, Java and C#
One of my friends (visitor) asked me in comment section “how to swap two variables without using temp variable” and I thought of sharing that answer with all my friends (visitors). For the first I am also going to provide full code in Java and C# language and my future articles will have codes in Java and C# along with C language. Along with regular logic of swapping variable I will show you how to do that without using third variable in C, Java and C# (C-Sharp).
C program to swap two numbers using third (temp) variable
#include<stdio.h> #include<conio.h> void main() { int a=100, b=30, temp; clrscr(); temp = a; a = b; b = temp; printf("Swapping using third (temp) variable.\n\n"); printf("Value of a=%d and b=%d.", a,b); getch(); }
Explanation
This is an absolute beginner section and if you think you are good in C language then you can skip this section. Above is a simple c program to swap two numbers using temp variable. You must be thinking why I am explaining such an easy program. Actually I thought there is some important point which most of you are familiar with but still I feel for beginners it’s important.
Let’s start then, we are declaring three integer variable in line no 6 and I found that many people think variable name “temp” in different way. They think that “temp” is a magical thing rather than an integer variable. Let me tell you friends that is only name of the variable, we name variable according to its use in program. So don’t get confuse with such names. You just need to check its data type and all confusion will be clear.
Let me also tell you why I named that variable as “temp”. If you notice code block (line no 9-11), you will come to know that in line no. 9 we are assigning value of “a” to “temp” (i.e. temp=100, a =100 & b=30) variable and in very next line (line no. 10) we are assigning value of “b” to “a” (i.e. a=30, b =30 & temp=100) variable and in last line (line no. 11) we assigning value of “temp” to “b” variable (i.e. b=100, temp =100 & a=30). In this entire program “temp” variable is used to hold value of “a” so that we can use it value later (like in line no. 11). Because I used it to store value temporarily so I named it “temp”.
C language books
C program to swap two numbers without using third variable
#include<stdio.h> #include<conio.h> void main() { int a=100, b=30; clrscr(); a = a+b; b = a-b; a = a-b; printf("Swapping without using third variable (using + and -).\n\n"); printf("Value of a=%d and b=%d.", a,b); getch(); }
In above program to swap two numbers we are not using any third or temp variable like we did in our first program. Here we are only using two arithmetic operators (+ & -) to swap values between “x” and “y”. Let’s see how it works,
a=100; b=30; a = a+b; a = 100 + 30; a = 130; b = a-b; b = 130 - 30; b = 100; // b has value of a. a = a-b; a = 130 - 100; a = 30; // a has b's value.
I don’t think after that illustration you guys need any explanation on logical part of the program.
You would be happy when you will come to know that there is some other methods for swapping two numbers. Let me quickly show you the code.
Method 2 : Swap two numbers without third variable in C
#include<stdio.h> #include<conio.h> void main() { int a=100, b=30; clrscr(); a = a*b; b = a/b; a = a/b; printf("Swapping without using third variable (using * and /).\n\n"); printf("Value of a=%d and b=%d.", a,b); getch(); }
Swapping of two numbers in C using XOR (^) operator without third variable
#include<stdio.h> #include<conio.h> void main() { int a=2, b=4; clrscr(); a = a^b; b = a^b; a = a^b; printf("Swapping without using third variable (using ^).\n\n"); printf("Value of a=%d and b=%d.", a,b); getch(); }
I think Method 2 is easy enough and don’t need any explanation. But Method 3 requires some explanation, so I will explain here Method 3 and if you find difficulty with Method 2 you can always ask me.
In Method 3 I am using bitwise XOR operator for swapping variable’s value. I hope all you guys are aware of bitwise binary operator like NOT, AND, OR etc. Because we are going to use that here, don’t worry guys I’ll try to “Keep It Simple and Stupid”. Just to refresh your memory about XOR below I provided truth table of XOR operator. Basically we are going see how XOR helps us to swap variable.
For illustration purpose I am taking small values in variable “a” and “b”.
XOR Truth Table
A
| B
| Result (A XOR B)
|
---|---|---|
0
| 0
| 0
|
0
| 1
| 1
|
1
| 0
| 1
|
1
| 1
| 0
|
Explanation:
a=2, b=4;
a=a^b;
0010 (equals to 2)
0100 (equals to 4)
------ (After XOR)
0110 (equals to 6)
a=6;
b=a^b;
0110 (equals to 6)
0100 (equals to 4)
------ (After XOR)
0010 (equals to 2)
b=2;
a=a^b;
0110 (equals to 6)
0010 (equals to 2)
------ (After XOR)
0100 (equals to 4)
a=4;
When we do XOR between “a & b” (i.e. a^b or 2^4) we get “6” (i.e. a=6, check the first code block). In the next C code block we are doing the same XOR between “a & b” but here value of “a” is “6”, so (i.e. a^b or 6^4) we get “2” (i.e. b=2). Similarly when we do XOR between “a & b” in the last block (i.e. a^b or 6^2) we get “4” (i.e. a=4). This is how we can swap variable’s value using XOR operation. I hope it is clear for you.
Simple method to swap two numbers in C#
using System; namespace SwapVariables { class Method1 { static void Main(string[] args) { int a = 100, b = 30; a = a + b; b = a - b; a = a - b; Console.WriteLine("Swapping without using third variable (using + and -).\n\n"); Console.WriteLine("Value of a = {0} and b= {1}.", a, b); Console.ReadKey(); } } }
Simple but different approach to swap two numbers in C#
using System; namespace SwapVariables { class Method2 { static void Main(string[] args) { int a = 100, b = 30; a = a * b; b = a / b; a = a / b; Console.WriteLine("Swapping without using third variable (using * and /).\n\n"); Console.WriteLine("Value of a = {0} and b= {1}.", a, b); Console.ReadKey(); } } }
Swapping of two numbers in C# using XOR (^) operator
using System; namespace SwapVariables { class Method3 { static void Main2(string[] args) { int a = 100, b = 30; a = a ^ b; b = a ^ b; a = a ^ b; Console.WriteLine("Swapping without using third variable (using ^).\n"); Console.WriteLine("Value of a = {0} and b= {1}.", a, b); Console.ReadKey(); } } }
Simple method to swap two numbers in Java
public class Method1 { public static void main(String[] args) { int a = 100, b = 30; a = a + b; b = a - b; a = a - b; System.out.println("Swapping without using third variable (using + and -).\n"); System.out.println("Value of a = " + a + " and b= " + b); } }
Simple but different approach to swap two numbers in Java
public class Method2 { public static void main(String[] args) { int a = 100, b = 30; a = a * b; b = a / b; a = a / b; System.out.println("Swapping without using third variable (using * and /).\n"); System.out.println("Value of a = " + a + " and b= " + b); } }
Swapping of two numbers in Java using XOR (^) operator
public class Method3 { public static void main(String[] args) { int a = 100, b = 30; a = a ^ b; b = a ^ b; a = a ^ b; System.out.println("Swapping without using third variable (using ^).\n"); System.out.println("Value of a = " + a + " and b= " + b); } }
Share your opinion with me
Did this article help you to understand topic (How to swap two numbers without using third (temp) variable.)?
Useful tutorials
- A Brief History of the C Language
Before we start any complex program in C, we must understand what really C is, how it came into existence and how it differs from other languages of that time. In this tutorial I will try to talk about these... - Data Types in C Language
A programming language is proposed to help programmer to process certain kinds of data and to provide useful output. The task of data processing is accomplished by executing series of commands called... - Array in C programming – Programmer's view
An array in C language is a collection of similar data-type, means an array can hold value of a particular data type for which it has been declared. Arrays can be created from any of the C data-types int, float, and char. So an integer array can... - What is Function in C Language?
A function in C language is a block of code that performs a specific task. It has a name and it is reusable i.e. it can be executed from as many different parts in a C Program as required. It also optionally returns a value to the calling program.... - Types of Function in C Programming Languages:
In my previous c programming tutorial I tried to explain what the function, its advantages is and how to declare a C function. And I told you that there are five types of functions and they are:
© 2011 RAJKISHOR SAHU