Thursday , 19 October 2017
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
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int a,b;
            System.out.print("Enter the 1st no: ");
            a=Integer.parseInt(br.readLine());
            System.out.print("Enter the 2nd no: ");
            b=Integer.parseInt(br.readLine());

            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]

Check Also

[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

5 comments

  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.

    • Here is Your answer Bharat: http://www.guideforschool.com/12128-swapping-two-strings-without-using-third-variable/

      For your convenience we are adding the code here also:

      /**
       * 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
          {
              BufferedReader br=new BufferedReader (new InputStreamReader (System.in));
              System.out.print("Enter the 1st String : ");
              String s1=br.readLine();
              int len1=s1.length();
              System.out.print("Enter the 2nd String : ");
              String s2=br.readLine();
      
              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.

Leave a Reply to Java For School Cancel reply

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