Basic Java Examples

Swap Numbers Without Using Third Variable Java Example

Want to learn quickly?
Try one of the many quizzes. More than Java 400 questions with detailed answers.

25 Comments

  • Couple of minor errors there:

    System.out.println(“Value of num2 is :” +num2); should have a space after the “+” as System.out.println(“Value of num2 is :” + num2);

    AND

    System.out.println(“Before Swapping”); on line 23 should be System.out.println(“After Swapping”);

    • it ll not be considered as an error if there is a space unavailable between the “+” operator and the variable name

          • import java.util.*;

            class swap1

            {

            public static void main(String arg[])

            {

            int a,b;

            Scanner X= new Scanner(System.in);

            System.out.print(“enter the value of a:\t”);

            a=X.nextInt();

            System.out.print(“enter the value of b:\t”);

            b=X.nextInt();

            System.out.println(“value of a and b before swapping:\t”+a+” and” +b);

            {

            a=a+b;

            b=a-b;

            a=a-b;

            System.out.println(“value of a and b after swapping:\t” +a+” and” +b);

            }

            }

            }

  • import java.util.*;

    public class Sriram
    {
    public static void main(String[] args)
    {
    change s= new change();
    s.operation();
    System.out.println(“After swapping, num1= ” + num1 + ” and num2= ” + num2);
    }

    class change
    {

    public void operation()
    {
    int num1=10;
    int num2=15;
    num1 = num1+num2;
    num2 = num1-num2;
    num1 = num1-num2;
    }
    }
    }
    will this program execute correctly

    • public class Ex4
      {
      public static void main(String[] args)
      {
      change s= new change();
      s.operation();
      }
      static class change
      {
      public void operation()
      {
      int num1=10;
      int num2=15;
      System.out.println(“before swapping, num1= ” + num1 + ” and num2= ” + num2);
      num1 = num1+num2;
      num2 = num1-num2;
      num1 = num1-num2;
      System.out.println(“After swapping, num1= ” + num1 + ” and num2= ” + num2);
      }
      }
      }

    • no, because the variables you are using are not known in class Sriram.
      their scope are only in operation class.so you will get compilation error.

  • num1 and num2 are local variables so cannot be referenced from outside operation function of change class to execute it we need to declare both num1 and num2 as global variables.
    and print s.num1,s.num2.

  • pls tell me…
    to no.
    A=10;
    B=20;

    10=10+20;
    20=10-20;
    10=10-20;
    …………????????????????
    aage kaise change hoga pls help me…?
    program mein to chal jayega pr
    10=10-20
    isko kaise solve kre aage?????

    • variable use karo ,,,, iha par variable ka value change ho raha he……
      initial value
      a=10;
      b=20;

      after + / –
      a=a+b; // a=10+20=30
      b=a-b; //b=30-20=10 ; b=10
      a=a-b; // a=30-10=20; a=20

      hua na swapping ?????????????????? 🙂

    • alya bhai num1=num1+num2 means 10+15=25 so num1 is a 25. then num2=num1-num2 means 25-15 so num2 is 10 ane num1=num1-num2 means 25-10=15… so num1 and num2 is swaped

    • Hi Mr.Gupta
      Assume like is
      num1=10+20=30;\\now the value of num1 is 30
      num2=30-20=10;\\now the value of num2 is 10
      num1=30-10=20;\\now num1 is 20

      num1=20
      num2=10
      the values are swapped.

    • a=10;
      b=20;
      a=10+20; //a becomes 30
      b=30-20; //b becomes 10
      a=30-10; //finally a becomes 20

      now a=20,b=10

      thats how it works…

    • By solving it one step at a time.
      A=10
      B=20
      Step 1 )A=A+B
      so, after this,
      A=30
      B=20
      Step 2) B=A-B
      so, after this,
      A=30
      B=10
      Step 3) A=A-B
      so, after this,
      A=20
      B=10
      [ Hence, swapped]

  • awsome!!!! greatly simple algorithm~
    i never thought this ever~
    but I was wondering its working as well in minus Integers.
    um..
    like below
    a = -10;
    b = -30;

    a = -40;
    b = -40 + 30 = – 10;
    a = -40 + 10 = – 30;

    and the conclusion is that stil working~
    Thanks for sharing your fabulous thinking.

  • I would like to know how to swap two numbers by using the XOR operator, i.e. without using any third variable or arithmetical operator. Please help me by replying with a JAVA code for the question.

  • import java.io.BufferedReader;

    import java.io.InputStreamReader;

    public class Swap_Number {

    public static void main(String[] args) {

    // TODO Auto-generated method stub

    int num1,num2;

    String s1,s2;

    try {

    BufferedReader BR= new BufferedReader(new InputStreamReader(System.in));

    System.out.print(“Enter ur 1st number: “);

    s1=BR.readLine();

    num1=Integer.parseInt(s1);

    System.out.print(“Enter ur 2st number: “);

    s2=BR.readLine();

    num2=Integer.parseInt(s2);

    num1=num1+num2;

    num2=num1-num2;

    num1=num1-num2;

    System.out.print(“1st number converted in 2nd number: “+num1);

    System.out.print(“\n2nd number converted in 1st number: “+num2);

    } catch (Exception e) {

    System.out.print(“wrong input”);

    // TODO: handle exception

    }

    }

    op: Enter ur 1st number: 100

    Enter ur 2st number: 12

    1st number converted in 2nd number: 12

    2nd number converted in 1st number: 100

  • import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;

    public class SwapTwo {

    public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub

    @SuppressWarnings(“resource”)
    Scanner sc = new Scanner(new InputStreamReader(System.in));
    int a, b;
    System.out.println(“Enter 2 values to swap”);
    a = sc.nextInt();
    b = sc.nextInt();

    System.out.println( ” Before swap Values are ” + a + ” ” + b);
    b = (a – b) + (a = b);

    System.out.println(” After swap Values are ” + a + ” ” + b);
    }

    }

    Output:

    Enter 2 values to swap
    54545 84545
    Before swap Values are 54545 84545
    After swap Values are 84545 54545

Sponsors

Facebook Fans