Skip to main content

Swap three variables without using fourth variable

Here given code implementation process.

// C Program
// Swap three variables without using fourth variable
#include <stdio.h>

// Swap three variables x y z
void swap(int *x, int *y, int *z)
{
	// Display given number
	printf("\n Before x : %d y : %d  z : %d", *x, *y, *z);
	// Find xor of given three numbers
	*x = *x ^ *y ^ *z;
	// Assign  x value to y
	*y = *x ^ *y ^ *z;
	// Assign  y value to z
	*z = *x ^ *y ^ *z;
	// Assign  z value to x
	*x = *x ^ *y ^ *z;
}
int main(int argc, char
	const *argv[])
{
	int x = 6;
	int y = 8;
	int z = 4;
	swap( & x, & y, & z);
	// Display swap result

	printf("\n  After x : %d y : %d  z : %d\n", x, y, z);
	// Reset new value
	x = 7;
	y = 3;
	z = 5;
	swap( & x, & y, & z);
	// Display swap result
	printf("\n  After x : %d y : %d  z : %d\n", x, y, z);
	return 0;
}

Output

 Before x : 6 y : 8  z : 4
  After x : 4 y : 6  z : 8

 Before x : 7 y : 3  z : 5
  After x : 5 y : 7  z : 3
/*
  Java program
  Swap three variables without using fourth variable
*/
public class SwapOperation
{
	public int x;
	public int y;
	public int z;
	public SwapOperation()
	{
		this.x = 0;
		this.y = 0;
		this.z = 0;
	}
	// Swap three variables x y z
	public void swap()
	{
		// Display given number
		System.out.print("\n Before x : " + x + " y : " + y + " z : " + z);
		// Find xor of given three numbers
		x = x ^ y ^ z;
		// Assign  x value to y
		y = x ^ y ^ z;
		// Assign  y value to z
		z = x ^ y ^ z;
		// Assign  z value to x
		x = x ^ y ^ z;
	}
	public static void main(String[] args)
	{
		SwapOperation task = new SwapOperation();
		task.x = 6;
		task.y = 8;
		task.z = 4;
		// Test
		task.swap();
		System.out.print("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
		// Reset new value
		task.x = 7;
		task.y = 3;
		task.z = 5;
      	task.swap();
		System.out.print("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
	}
}

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
// Include header file
#include <iostream>

using namespace std;
/*
  C++ program
  Swap three variables without using fourth variable
*/
class SwapOperation
{
  	public:
    // Swap three variables x y z
    void swap(int &x,int &y, int &z)
    {
        // Display given number
        cout << "\n Before x : " << x << " y : " << y << " z : " << z;
        // Find xor of given three numbers
        x = x ^ y ^ z;
        // Assign  x value to y
        y = x ^ y ^ z;
        // Assign  y value to z
        z = x ^ y ^ z;
        // Assign  z value to x
        x = x ^ y ^ z;
    }
};
int main()
{
    SwapOperation task = SwapOperation();
    int x = 6;
    int y = 8;
    int z = 4;
    // Test
    task.swap(x,y,z);
    cout << "\n  After x : " << x << " y : " << y << " z : " << z;
    // Reset new value
    x = 7;
    y = 3;
    z = 5;
    task.swap(x,y,z);
    cout << "\n  After x : " << x << " y : " << y << " z : " << z;
    return 0;
}

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
// Include namespace system
using System;
/*
  C# program
  Swap three variables without using fourth variable
*/
public class SwapOperation
{

	// Swap three variables x y z
	public void swap(ref int x, ref int y, ref int z)
	{
		// Display given number
		Console.Write("\n Before x : " + x + " y : " + y + " z : " + z);
		// Find xor of given three numbers
		x = x ^ y ^ z;
		// Assign  x value to y
		y = x ^ y ^ z;
		// Assign  y value to z
		z = x ^ y ^ z;
		// Assign  z value to x
		x = x ^ y ^ z;
	}
	public static void Main(String[] args)
	{
		SwapOperation task = new SwapOperation();
		int x = 6;
		int y = 8;
		int z = 4;
		// Test
		task.swap(ref x,ref y,ref z);
		Console.Write("\n  After x : " + x + " y : " + y + " z : " + z);
		// Reset new value
		x = 7;
		y = 3;
		z = 5;
		task.swap(ref x,ref y,ref z);
		Console.Write("\n  After x : " + x + " y : " + y + " z : " + z);
	}
}

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
<?php
/*
  Php program
  Swap three variables without using fourth variable
*/
class SwapOperation
{
	// Swap three variables x y z
	public	function swap(&$x, &$y, &$z)
	{
		// Display given number
		echo "\n Before x : ". $x ." y : ". $y ." z : ". $z;
		// Find xor of given three numbers
		$x = $x ^ $y ^ $z;
		// Assign  x value to y
		$y = $x ^ $y ^ $z;
		// Assign  y value to z
		$z = $x ^ $y ^ $z;
		// Assign  z value to x
		$x = $x ^ $y ^ $z;
	}
}

function main()
{
	$task = new SwapOperation();
	$x = 6;
	$y = 8;
	$z = 4;
	// Test
	$task->swap($x, $y, $z);
	echo "\n  After x : ". $x ." y : ". $y ." z : ". $z;
	// Reset new value
	$x = 7;
	$y = 3;
	$z = 5;
	$task->swap($x, $y, $z);
	echo "\n  After x : ". $x ." y : ". $y ." z : ". $z;
}
main();

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
/*
  Node Js program
  Swap three variables without using fourth variable
*/
class SwapOperation
{
	constructor()
	{
		this.x = 0;
		this.y = 0;
		this.z = 0;
	}
	// Swap three variables x y z
	swap()
	{
		// Display given number
		process.stdout.write("\n Before x : " + this.x + " y : " + this.y + " z : " + this.z);
		// Find xor of given three numbers
		this.x = this.x ^ this.y ^ this.z;
		// Assign  x value to y
		this.y = this.x ^ this.y ^ this.z;
		// Assign  y value to z
		this.z = this.x ^ this.y ^ this.z;
		// Assign  z value to x
		this.x = this.x ^ this.y ^ this.z;
	}
}

function main()
{
	var task = new SwapOperation();
	task.x = 6;
	task.y = 8;
	task.z = 4;
	// Test
	task.swap();
	process.stdout.write("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
	// Reset new value
	task.x = 7;
	task.y = 3;
	task.z = 5;
  	task.swap();
	process.stdout.write("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
}
main();

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
#   Python 3 program
#   Swap three variables without using fourth variable

class SwapOperation :
	
	def __init__(self) :
		self.x = 0
		self.y = 0
		self.z = 0
	
	#  Swap three variables x y z
	def swap(self) :
		#  Display given number
		print("\n Before x : ", self.x ," y : ", self.y ," z : ", self.z, end = "")
		#  Find xor of given three numbers
		self.x = self.x ^ self.y ^ self.z
		#  Assign  x value to y
		self.y = self.x ^ self.y ^ self.z
		#  Assign  y value to z
		self.z = self.x ^ self.y ^ self.z
		#  Assign  z value to x
		self.x = self.x ^ self.y ^ self.z
	

def main() :
	task = SwapOperation()
	task.x = 6
	task.y = 8
	task.z = 4
	#  Test
	task.swap()
	print("\n  After x : ", task.x ," y : ", task.y ," z : ", task.z, end = "")
	#  Reset new value
	task.x = 7
	task.y = 3
	task.z = 5
	task.swap()
	print("\n  After x : ", task.x ," y : ", task.y ," z : ", task.z, end = "")

if __name__ == "__main__": main()

Output

 Before x :  6  y :  8  z :  4
  After x :  4  y :  6  z :  8
 Before x :  7  y :  3  z :  5
  After x :  5  y :  7  z :  3
#   Ruby program
#   Swap three variables without using fourth variable

class SwapOperation  
	# Define the accessor and reader of class SwapOperation  
	attr_reader :x, :y, :z
	attr_accessor :x, :y, :z
 
	
	def initialize() 
		self.x = 0
		self.y = 0
		self.z = 0
	end

	#  Swap three variables x y z
	def swap() 
		#  Display given number
		print("\n Before x : ", self.x ," y : ", self.y ," z : ", self.z)
		#  Find xor of given three numbers
		self.x = self.x ^ self.y ^ self.z
		#  Assign  x value to y
		self.y = self.x ^ self.y ^ self.z
		#  Assign  y value to z
		self.z = self.x ^ self.y ^ self.z
		#  Assign  z value to x
		self.x = self.x ^ self.y ^ self.z
	end

end

def main() 
	task = SwapOperation.new()
	task.x = 6
	task.y = 8
	task.z = 4
	#  Test
	task.swap()
	print("\n  After x : ", task.x ," y : ", task.y ," z : ", task.z)
	#  Reset new value
	task.x = 7
	task.y = 3
	task.z = 5
	task.swap()
	print("\n  After x : ", task.x ," y : ", task.y ," z : ", task.z)
end

main()

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3
/*
  Scala program
  Swap three variables without using fourth variable
*/
class SwapOperation(var x: Int , var y: Int , var z: Int)
{
	def this()
	{
		this(0, 0, 0);
	}
	// Swap three variables x y z
	def swap(): Unit = {
		// Display given number
		print("\n Before x : " + x + " y : " + y + " z : " + z);
		// Find xor of given three numbers
		x = x ^ y ^ z;
		// Assign  x value to y
		y = x ^ y ^ z;
		// Assign  y value to z
		z = x ^ y ^ z;
		// Assign  z value to x
		x = x ^ y ^ z;
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: SwapOperation = new SwapOperation();
		task.x = 6;
		task.y = 8;
		task.z = 4;
		// Test
		task.swap();
		print("\n After x : " + task.x + " y : " + task.y + " z : " + task.z);
		// Reset new value
		task.x = 7;
		task.y = 3;
		task.z = 5;
		task.swap();
		print("\n After x : " + task.x + " y : " + task.y + " z : " + task.z);
	}
}

Output

 Before x : 6 y : 8 z : 4
 After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
 After x : 5 y : 7 z : 3
/*
  Swift 4 program
  Swap three variables without using fourth variable
*/
class SwapOperation
{
	// Swap three variables x y z
	func swap(_ x: inout Int, _ y: inout Int, _ z: inout Int)
	{
		// Display given number
		print("\n Before x : ", x ," y : ", y ," z : ", z, terminator: "");
		// Find xor of given three numbers
		x = x ^ y ^ z;
		// Assign  x value to y
		y = x ^ y ^ z;
		// Assign  y value to z
		z = x ^ y ^ z;
		// Assign  z value to x
		x = x ^ y ^ z;
	}
}
func main()
{
	let task: SwapOperation = SwapOperation();
	var x: Int = 6;
	var y: Int = 8;
	var z: Int = 4;
	// Test
	task.swap(&x, &y, &z);
	print("\n  After x : ", x ," y : ", y ," z : ", z, terminator: "");
	// Reset new value
	x = 7;
	y = 3;
	z = 5;
	task.swap(&x, &y, &z);
	print("\n  After x : ", x ," y : ", y ," z : ", z, terminator: "");
}
main();

Output

 Before x :  6  y :  8  z :  4
  After x :  4  y :  6  z :  8
 Before x :  7  y :  3  z :  5
  After x :  5  y :  7  z :  3
/*
  Kotlin program
  Swap three variables without using fourth variable
*/
class SwapOperation
{
	var x: Int;
	var y: Int;
	var z: Int;
	constructor()
	{
		this.x = 0;
		this.y = 0;
		this.z = 0;
	}
	// Swap three variables x y z
	fun swap(): Unit
	{
		// Display given number
		print("\n Before x : " + x + " y : " + y + " z : " + z);
		// Find xor of given three numbers
		x = x xor y xor z;
		// Assign x value to y
		y = x xor y xor z;
		// Assign y value to z
		z = x xor y xor z;
		// Assign z value to x
		x = x xor y xor z;
	}
}
fun main(args: Array < String > ): Unit
{
	var task: SwapOperation = SwapOperation();
	task.x = 6;
	task.y = 8;
	task.z = 4;
	// Test
	task.swap();
	print("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
	// Reset new value
	task.x = 7;
	task.y = 3;
	task.z = 5;
	task.swap();
	print("\n  After x : " + task.x + " y : " + task.y + " z : " + task.z);
}

Output

 Before x : 6 y : 8 z : 4
  After x : 4 y : 6 z : 8
 Before x : 7 y : 3 z : 5
  After x : 5 y : 7 z : 3




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment