Friday , 18 August 2017
Latest Posts
Home » Class 10 » Swapping two numbers without using third variable [Method 2]

# Swapping two numbers without using third variable [Method 2]

This is the Java programming code written in BlueJ which swaps the values of two numbers without using any third variable.

This is the second method in which we have used the concept of simple mathematical operations including addition and subtraction.

Method 1 of swapping two numbers using bitwise XOR operator and without using any third variable can be read from here: [Method 1]

#### Programming Code:

```/**
* The class Swapping_Method2 takes 2 numbers as input and swaps their value without using any 3rd variable
* This is Method 2
* @author : www.javaforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.io.*;
class Swapping_Method2
{
public static void main(String args[])throws IOException
{
int a,b;
System.out.print("Enter the 1st no: ");
System.out.print("Enter the 2nd no: ");

System.out.println("-------------------------------");
System.out.println("The numbers before swapping are");
System.out.println("a = "+a);
System.out.println("b = "+b);

//Beginning of Swapping
a=a+b;
b=a-b;
a=a-b;

//End of Swapping
System.out.println("-------------------------------");
System.out.println("The numbers after swapping are");
System.out.println("a = "+a);
System.out.println("b = "+b);
}
}```

#### Output:

Enter the 1st no: 25
Enter the 2nd no: 13
——————————-
The numbers before swapping are
a = 25
b = 13
——————————-
The numbers after swapping are
a = 13
b = 25

#### Working:

Initially a=25 and b=13,

Step 1: a=a+b gives a=25+13
i.e. a=38
Step 2: b=a-b gives, b=38-13
i.e. b=25
Step 3: a=a-b gives, a=38-25
i.e. a=13

Hence, finally we have a=13 and b=25. [Swapping Done]

## [Question 2] ISC 2017 Computer Practical Paper Solved – Quiz Result

Solution of Program 2 of ISC 2017 Computer Science Paper 2 (Practical) Exam. Java program to input the answers of each participant row-wise and calculate their marks

1. Thanks for the solution 😀

2. can you help same like swapping the two strings without using temp variable and without using swap method.Please help me any one.

```/**
* The class Swap_Strings takes 2 Strings as input
* and swaps their value without using any 3rd variable
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.io.*;
class Swap_Strings
{
public static void main(String args[])throws IOException
{
System.out.print("Enter the 1st String : ");
int len1=s1.length();
System.out.print("Enter the 2nd String : ");

System.out.println("-------------------------------");
System.out.println("Strings Before Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);

/*Swapping Process Begins*/
s1=s1+s2;
s2=s1.substring(0,len1);
s1=s1.substring(len1);
/*Swapping Process Ends*/

System.out.println("-------------------------------");
System.out.println("Strings After Swapping : ");
System.out.println("1st String = "+s1);
System.out.println("2nd String = "+s2);
}
}```

In order to know the working of this program, kindly visit the above link.