Tuesday , December 6 2016
Home / Blue J / Java program to swap two numbers without using a third variable

Java program to swap two numbers without using a third variable

Here are 4 Tricks to swap two numbers without using a third variable.

Here is a java program to swap two numbers without using a third variable. To do so you can use basic mathematical operations like multiplication, division and addition, subtraction. See the Further Explanation: 

Trick #1

 
  Trick #1: Addition, subtraction

A=A+B;
B=A-B;
A=A-B

 Program #1: Swap two numbers without using third variable using addition and subtraction.

}

 Trick #2

 
 Trick #2: Multiplication, Division.

A=A X B;
B=A/B;
A=A/B;

Program #2: Swap two numbers without using third variable using Multiplication and division 

 Trick #3

 
 Trick #3: XOR Operation,
A=A^B;
B=A^B;
A=A^B; 

Program #3: Swap two numbers without using third variable : XOR operation trick.

 

 Trick #4

 
 Trick #4: Basic logic: 1 Line Trick,

A=A+B – (B=A); 

Program #4Swap two numbers without using third variable: Basic Logic; 1 Line trick. 

Incoming terms: tricks to swap two numbers, swap two numbers various tricks.

Also See:   Java program for armstrong number using recursion

Check Also

Program for Matrix Multiplication in JAVA

Matrix Multiplication:– Here is given a Java Program for Matrix Multiplication also call matrix product. In ...

Leave a Reply

Your email address will not be published. Required fields are marked *

Preventing spam * Time limit is exhausted. Please reload the CAPTCHA.