swap 2 integers without using temporary variable
using System;
class MainClass {
public static void Main (string[] args) {
int num1 = int.Parse(Console.ReadLine());
int num2 = int.Parse(Console.ReadLine());
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
Console.WriteLine("After swapping: num1 = "+ num1 + ", num2 = " + num2);
Console.ReadLine();
}
}
// 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 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.