swap two numbers without using temp
// Java Program to swap two numbers without
// using temporary variable
import java.*;
class Geeks {
public static void main(String a[])
{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by Sam007
// Java code to swap using XOR
import java.*;
public class GFG {
public static void main(String a[])
{
int x = 10;
int y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
System.out.println("After swap: x = "
+ x + ", y = " + y);
}
}
// This code is contributed by Sam007.
// Java Program to swap two numbers
// without using temporary variable
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x = 10;
int y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit
// C++ Program to swap two numbers
// without using temporary variable
#include <bits/stdc++.h>
using namespace std;
int main()
{//NOTE - for this code to work in a generalised sense, y !- 0 to prevent zero division
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 15
y = x / y; // y becomes 10
x = x / y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}
// This code is contributed by mohit kumar.