## How do you swap two values without using a third variable?

**Program to swap two numbers without using the third variable**

- STEP 1: START.
- STEP 2: ENTER x, y.
- STEP 3: PRINT x, y.
- STEP 4: x = x + y.
- STEP 5: y= x – y.
- STEP 6: x =x – y.
- STEP 7: PRINT x, y.
- STEP 8: END.

## How can I swap two variables without using third variable in Java?

**Swap two numbers without using third variable in java**

- class demo {
- public static void main(string arg[]) {
- System.out.println(“Before swapping”);
- int x = 10;
- int y = 20;
- System.out.println(“value of x:” + x);
- System.out.println(“value of y:” + y);
- system.out.println(“After swapping”);

## How can you swap two numbers without using a third variable using XOR?

**Bitwise operators** can also be used to swap two numbers without using a third variable. XOR bitwise operator returns zero if both operands are the same i.e. either 0 or 1 and return 1 if both operands are different e.g. one operand is zero and the other is one.

## How do you swap two numbers without temp variables?

**C Program to swap two numbers without third variable**

- #include<stdio.h>
- int main()
- {
- int a=10, b=20;
- printf(“Before swap a=%d b=%d”,a,b);
- a=a+b;//a=30 (10+20)
- b=a-b;//b=10 (30-20)
- a=a-b;//a=20 (30-10)

## How do you swap two variables?

**The bitwise XOR operator** can be used to swap two variables. The XOR of two numbers x and y returns a number that has all the bits as 1 wherever bits of x and y differ. For example, XOR of 10 (In Binary 1010) and 5 (In Binary 0101) is 1111 and XOR of 7 (0111) and 5 (0101) is (0010).

## What is swapping of two numbers?

Swap two numbers means **exchange the values of two variables with each other**. For example variable num1 contains 20 and num2 contains 40 after swap there values num1 contains 40 and num2 contains 20.

## How can I swap two numbers without third variable in C++?

**C++ Program to swap two numbers without third variable**

- #include <iostream>
- using namespace std;
- int main()
- {
- int a=5, b=10;
- cout<<“Before swap a= “<<a<<” b= “<<b<<endl;
- a=a*b; //a=50 (5*10)
- b=a/b; //b=5 (50/10)

## How can we swap two numbers without third variable and without arithmetic operator?

**The arithmetic operators for addition and subtraction** can be used to perform the swap without using a third variable. Similarly, multiplication and division can be used to perform the swap without using the third variable.

## How do you swap two variables with a third variable?

**Swapping Two Numbers Using Third Variable**

- Assign var1 value to a temp variable: temp = var1.
- Assign var2 value to var1: var1 = var2.
- Assign temp value to var2: var2 = temp.

## How do you swap two variables in Java?

**Java Program to Swap two Variables**

- Assign x to a temp variable : temp = x.
- Assign y to x : x = y.
- Assign temp to y : y = temp.

## Can you swap 2 strings in Java?

To swap two string in Java Programming, you have to **first ask to the user to enter the two string, then make a temp variable of the same type**. Now place the first string in the temp variable, then place the second string in the first, now place the temp string in the second.

## How do you swap two arrays?

To swap elements of two arrays you have **to swap each pair of elemenets separatly**. And you have to supply the number of elements in the arrays. Otherwise the arrays need to have a sentinel value. Here is a demonstrative program that shows how the function swap can be defined.