¿Cómo intercambiar dos números sin usar una variable temporal?

 

Dadas dos variables, x e y, intercambie dos variables sin usar una tercera variable. 

C++

// C++ Program to swap two numbers  without
// using temporary variable
#include <bits/stdc++.h>
using namespace std;
 
int main()
{
    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.

C

#include <stdio.h>
int main()
{
    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
 
    printf("After Swapping: x = %d, y = %d", x, y);
 
    return 0;
}

Java

// Java Program to swap two numbers  without
// using temporary variable
import java.io.*;
 
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 swapping:"
                           + " x = " + x + ", y = " + y);
    }
}
 
// This code is contributed by Mayank Tyagi

Python3

x = 10
y = 5
 
# Code to swap 'x' and 'y'
 
# x now becomes 15
x = x + y
 
# y becomes 10
y = x - y
 
# x becomes 5
x = x - y
print("After Swapping: x =", x, " y =", y)
 
# This code is contributed
# by Sumit Sudhakar

C#

// Program to swap two numbers  without
// using temporary variable
using System;
 
class GFG {
    public static void Main()
    {
        int x = 10;
        int y = 5;
 
        x = x + y;
        y = x - y;
        x = x - y;
        Console.WriteLine("After swapping: x = " + x
                          + ", y = " + y);
    }
}
 
// This code is contributed by Sam007

PHP

<?php
// PHP Program to swap two
// numbers without using
// temporary variable
$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
 
echo "After Swapping: x = ",
       $x, ", ", "y = ", $y;
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
// Javascript program to swap two
// numbers without using temporary
// variable
 
let x = 10, y = 5;
 
// Code to swap 'x' and 'y'
 
// x now becomes 15
x = x + y;
 
// y becomes 10
y = x - y;
 
// x becomes 5
x = x - y;
 
document.write("After Swapping: x =" + x + ", y=" + y);
 
// This code is contributed by mukesh07
 
</script>

C++

// 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 50
    y = x / y; // y becomes 10
    x = x / y; // x becomes 5
    cout << "After Swapping: x =" << x << ", y=" << y;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C Program to swap two numbers without using temporary
// variable
#include <stdio.h>
int main()
{
    int x = 10, 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
 
    printf("After Swapping: x = %d, y = %d", x, y);
 
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

// 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 Aditya Kumar (adityakumar129)

Python3

# Python3 program to
# swap two numbers
# without using
# temporary variable
x = 10
y = 5
 
# code to swap
# 'x' and 'y'
 
# x now becomes 50
x = x * y
 
# y becomes 10
y = x // y;
 
# x becomes 5
x = x // y;
 
print("After Swapping: x =",
              x, " y =", y);
 
# This code is contributed
# by @ajit

C#

// C# Program to swap two
// numbers without using
// temporary variable
using System;
 
class GFG {
    static public void Main()
    {
        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
 
        Console.WriteLine("After swaping:"
                          + " x = " + x + ", y = " + y);
    }
}
 
// This code is contributed by ajit.

PHP

<?php
// Driver code
    $x = 10;
    $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
 
echo "After Swapping: x = ", $x,
                " ", "y = ", $y;
 
// This code is contributed by m_kit
?>

Javascript

<script>
 
// Javascript program to swap two numbers
// without using temporary variable
var x = 10;
var 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
 
document.write("After swaping:" + " x = " +
               x + ", y = " + y);
 
// This code is contributed by shikhasingrajput
 
</script>

C++

// C++ code to swap using XOR
#include <bits/stdc++.h>
 
using namespace std;
 
int main()
{
    int x = 10, 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)
    cout << "After Swapping: x =" << x << ", y=" << y;
    return 0;
}
 
// This code is contributed by mohit kumar.

C

// C code to swap using XOR
#include <stdio.h>
int main()
{
    int x = 10, 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)
 
    printf("After Swapping: x = %d, y = %d", x, y);
 
    return 0;
}

Java

// Java code to swap using XOR
import java.io.*;
 
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 Mayank Tyagi

Python3

# Python3 code to swap using XOR
 
x = 10
y = 5
 
# Code to swap 'x' and 'y'
x = x ^ y; # x now becomes 15 (1111)
y = x ^ y; # y becomes 10 (1010)
x = x ^ y; # x becomes 5 (0101)
 
print ("After Swapping: x = ", x, " y =", y)
 
# This code is contributed by
# Sumit Sudhakar

C#

// C# program to swap using XOR
using System;
 
class GFG {
    public static void Main()
    {
        int x = 10;
        int y = 5;
 
        // Code to swap 'x' (1010)
        // and 'y' (0101)
 
        // x now becomes 15 (1111)
        x = x ^ y;
 
        // y becomes 10 (1010)
        y = x ^ y;
 
        // x becomes 5 (0101)
        x = x ^ y;
 
        Console.WriteLine("After swap: x = " + x + ", y = " + y);
    }
}
 
// This code is contributed by ajit

PHP

<?php
 
// Driver Code
$x = 10;
$y = 5;
 
// Code to swap 'x' (1010)
// and 'y' (0101)
 
// x now becomes 15 (1111)
$x = $x ^ $y;
 
// y becomes 10 (1010)
$y = $x ^ $y;
 
// x becomes 5 (0101)
$x = $x ^ $y;
 
echo "After Swapping: x = ", $x,
                ", ", "y = ", $y;
 
// This code is contributed by aj_36
?>

Javascript

<script>
 
// Javascript code to swap using XOR
 
let x = 10, 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)
 
document.write("After Swapping: x =" +
               x + ", y=" + y);
 
// This code is contributed by Mayank Tyagi
     
</script>

C++

#include <bits/stdc++.h>
using namespace std;
void swap(int* xp, int* yp)
{
    *xp = *xp ^ *yp;
    *yp = *xp ^ *yp;
    *xp = *xp ^ *yp;
}
 
// Driver code
int main()
{
    int x = 10;
    swap(&x, &x);
    cout << "After swap(&x, &x): x = " << x;
    return 0;
}
 
// This code is contributed by rathbhupendra

C

#include <stdio.h>
void swap(int* xp, int* yp)
{
    *xp = *xp ^ *yp;
    *yp = *xp ^ *yp;
    *xp = *xp ^ *yp;
}
 
int main()
{
    int x = 10;
    swap(&x, &x);
    printf("After swap(&x, &x): x = %d", x);
    return 0;
}

Java

class GFG {
    static void swap(int[] xp, int[] yp)
    {
        xp[0] = xp[0] ^ yp[0];
        yp[0] = xp[0] ^ yp[0];
        xp[0] = xp[0] ^ yp[0];
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int[] x = { 10 };
        swap(x, x);
        System.out.println("After swap(&x, &x): x = " + x[0]);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

def swap(xp, yp):
 
    xp[0] = xp[0] ^ yp[0]
    yp[0] = xp[0] ^ yp[0]
    xp[0] = xp[0] ^ yp[0]
 
 
# Driver code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
 
# This code is contributed by SHUBHAMSINGH10

C#

// C# program to implement
// the above approach
using System;
class GFG {
 
    static void swap(int[] xp, int[] yp)
    {
        xp[0] = xp[0] ^ yp[0];
        yp[0] = xp[0] ^ yp[0];
        xp[0] = xp[0] ^ yp[0];
    }
 
    // Driver code
    static void Main()
    {
        int[] x = { 10 };
        swap(x, x);
        Console.WriteLine("After swap(&x,"
                          + "&x): x = " + x[0]);
    }
}
 
// This code is contributed by divyeshrabadiya07

PHP

<?php
function swap(&$xp, &$yp)
{
    $xp = $xp ^ $yp;
    $yp = $xp ^ $yp;
    $xp = $xp ^ $yp;
}
 
// Driver Code
$x = 10;
swap($x, $x);
print("After swap(&x, &x): x = " . $x);
 
// This code is contributed
// by chandan_jnu
?>

Javascript

<script>
     
    function swap(xp,yp)
    {
        xp[0] = xp[0] ^ yp[0];
        yp[0] = xp[0] ^ yp[0];
        xp[0] = xp[0] ^ yp[0];
    }
     
    // Driver code
     
    let x=[10];
    swap(x, x);
    document.write("After swap(&x, &x): x = "
                           + x[0]);
     
     
    // This code is contributed by unknown2108
     
</script>

C++

#include <bits/stdc++.h>
using namespace std;
void swap(int* xp, int* yp)
{
 
    // Check if the two addresses are same
    if (xp == yp)
        return;
    *xp = *xp + *yp;
    *yp = *xp - *yp;
    *xp = *xp - *yp;
}
 
// Driver Code
int main()
{
    int x = 10;
    swap(&x, &x);
    cout << "After swap(&x, &x): x = " << x;
    return 0;
}
 
// This code is contributed by rathbhupendra

C

#include <stdio.h>
void swap(int* xp, int* yp)
{
    if (xp == yp) // Check if the two addresses are same
        return;
    *xp = *xp + *yp;
    *yp = *xp - *yp;
    *xp = *xp - *yp;
}
int main()
{
    int x = 10;
    swap(&x, &x);
    printf("After swap(&x, &x): x = %d", x);
    return 0;
}

Java

// Java program of above approach
class GFG {
 
    static void swap(int xp, int yp)
    {
        if (xp == yp) // Check if the two addresses are same
            return;
        xp = xp + yp;
        yp = xp - yp;
        xp = xp - yp;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int x = 10;
        swap(x, x);
        System.out.println("After swap(&x, &x): x = " + x);
    }
}
 
// This code is Contributed by Code_Mech.

Python3

# Python3 program of above approach
def swap(xp, yp):
 
    # Check if the two addresses are same
    if (xp[0] == yp[0]):
        return
    xp[0] = xp[0] + yp[0]
    yp[0] = xp[0] - yp[0]
    xp[0] = xp[0] - yp[0]
 
 
# Driver Code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
 
# This code is contributed by SHUBHAMSINGH10

C#

// C# program of above approach
using System;
class GFG {
 
    static void swap(int xp, int yp)
    {
        if (xp == yp) // Check if the two addresses are same
            return;
        xp = xp + yp;
        yp = xp - yp;
        xp = xp - yp;
    }
 
    // Driver code
    public static void Main()
    {
        int x = 10;
        swap(x, x);
        Console.WriteLine("After swap(&x, &x): x = " + x);
    }
}
 
// This code is Contributed by Code_Mech.

PHP

<?php
function swap($xp, $yp)
{
    // Check if the two addresses
    // are same
    if ($xp == $yp)
        return;
    $xp = $xp + $yp;
    $yp = $xp - $yp;
    $xp = $xp - $yp;
}
 
// Driver Code
$x = 10;
swap($x, $x);
echo("After swap(&x, &x): x = " . $x);
return 0;
 
// This code is contributed
// by Code_Mech.

Javascript

<script>
   function swap(xp, yp)
{
  
    // Check if the two addresses are same
    if (xp == yp)
        return;
    xp[0] = xp[0] + yp[0];
    yp[0] = xp[0] - yp[0];
    xp[0]= xp[0] - yp[0];
}
  
// Driver Code
     x = 10;
    swap(x, x);
    document.write("After swap(&x , &x) : x = " + x);
//This code is contributed by simranarora5sos
</script>

C++

// C++ program to swap two numbers
#include <bits/stdc++.h>
using namespace std;
 
// Function to swap the numbers.
void swap(int& a, int& b)
{
    // same as a = a + b
    a = (a & b) + (a | b);
    // same as b = a - b
    b = a + (~b) + 1;
    // same as a = a - b
    a = a + (~b) + 1;
}
 
// Driver Code
int main()
{
    int a = 5, b = 10;
    // Function Call
    swap(a, b);
    cout << "After swapping: a = " << a << ", b = " << b;
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

C

// C++ program to swap two numbers
#include <stdio.h>
 
// Function to swap the numbers.
void swap(int a, int b)
{
    // same as a = a + b
    a = (a & b) + (a | b);
    // same as b = a - b
    b = a + (~b) + 1;
    // same as a = a - b
    a = a + (~b) + 1;
      printf("After swapping: a = %d , b = %d ",a,b);
}
 
// Driver Code
int main()
{
    int a = 5, b = 10;
    // Function Call
    swap(a, b);
    return 0;
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Java

// Java program to swap two numbers
import java.io.*;
 
class GFG {
    public static void swap(int a, int b)
    {
        // same as a = a + b
        a = (a & b) + (a | b);
        // same as b = a - b
        b = a + (~b) + 1;
        // same as a = a - b
        a = a + (~b) + 1;
        System.out.print("After swapping: a = " + a + ", b = " + b);
    }
    public static void main(String[] args)
    {
        int a = 5, b = 10;
        // Function Call
        swap(a, b);
    }
}
 
// This code is contributed by Aditya Kumar (adityakumar129)

Python3

# Python3 program to swap two numbers
 
# Function to swap the numbers
 
 
def swap(a, b):
 
    # Same as a = a + b
    a = (a & b) + (a | b)
 
    # Same as b = a - b
    b = a + (~b) + 1
 
    # Same as a = a - b
    a = a + (~b) + 1
 
    print("After Swapping: a = ", a, ", b = ", b)
 
 
# Driver code
a = 5
b = 10
 
# Function call
swap(a, b)
 
# This code is contributed by bunnyram19

C#

// C# program to swap two numbers
using System;
class GFG {
 
    static void swap(int a, int b)
    {
        // same as a = a + b
        a = (a & b) + (a | b);
 
        // same as b = a - b
        b = a + (~b) + 1;
 
        // same as a = a - b
        a = a + (~b) + 1;
 
        Console.Write("After swapping: a = " + a
                      + ", b = " + b);
    }
 
    static void Main()
    {
        int a = 5, b = 10;
 
        // Function Call
        swap(a, b);
    }
}
 
// This code is contributed by divyesh072019

Javascript

<script>
    // Javascript program to swap two numbers
     
    function swap(a, b)
    {
        // same as a = a + b
        a = (a & b) + (a | b);
  
        // same as b = a - b
        b = a + (~b) + 1;
  
        // same as a = a - b
        a = a + (~b) + 1;
  
        document.write("After swapping: a = " + a + ", b = " + b);
    }
     
    let a = 5, b = 10;
  
    // Function Call
    swap(a, b);
     
    // This code is contributed by suresh07.
</script>

PHP

<?php
 
 
// Driver Code
$a = 5;
$b = 10;
 
echo("Before swap(a and b) " . $a . "and". $b."<br>");
// same as a = a + b
    $a = ($a & $b) + ($a | $b);
  
    // same as b = a - b
    $b = $a + (~$b) + 1;
  
    // same as a = a - b
    $a = $a + (~$b) + 1;
 
echo("After swap(a and b) " . $a. "and". $b);
return 0;
 
?>

C++

#include <iostream>
using namespace std;
  
int main(){
    int x = 10, y = 5;
    x = (x * y) / (y = x);
    cout << x << " " << y;
    return 0;
}
 
// This code is contributed by isha307

C

#include <stdio.h>
 
int main() {
    int x = 10, y = 5;
    x = (x * y) / (y = x);
    printf("After Swapping: x = %d, y = %d", x, y);
    return 0;
}
 
// This code is contributed by isha307

Java

/*package whatever //do not write package name here */
import java.io.*;
 
class GFG {
    public static void main(String[] args)
    {
        int x = 10;
        int y = 5;
        x = (x * y) / (y = x);
        System.out.println("After swaping:"
                           + " x = " + x + ", y = " + y);
    }
}
 
// This code is contributed by isha307

Python3

# Python3 program to swap two numbers
 
# Function to swap the numbers
def swap(x, y):
  x , y = y, x
  print("After Swapping: x = ", x, ", y = ", y)
   
# Driver code
x = 10
y = 5
  
# Function call
swap(x, y)
  
# This code is contributed by kothavvsaakash

C#

// C# program to swap two numbers
 
using System;
 
public class GFG
{
    static public void Main ()
    {
        int x = 10;
        int y = 5;
        x = (x * y) / (y = x);
        Console.Write("After swaping:"  + " x = " + x + ", y = " + y);
    }
}
 
// This code is contributed by kothavvsaakash

Javascript

<script>
 
// Javascript program to swap two
// numbers without using temporary
// variable
 
let x = 10, y = 5;
 
// Code to swap 'x' and 'y'
x = (x * y)/(x = y);
 
document.write("After Swapping: x =" + x + ", y=" + y);
 
// This code is contributed by Abhijeet Kumar(abhijeet19403)
 
</script>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *