Monday, January 8, 2024

SWAP THE NUMBER IN DIFFERENT WAYS .

 

              SWAPPING THE VARIABLES


What is swap?

                          Transfer the value of one variable to another and take the value of the second variable and put it to first. Simply explain (you have two variable var1 & var2. Value of var1 is 20 & value of var2 is 40. So, after swapping the value of var1 will become 40 & value of var2 will become 20.)


Why do we swap variables?

                             Swapping two variables in a code can be useful in various scenarios. One common reason is to reassign the values of the variables to each other, allowing for a more efficient way to organize and manipulate data. This can be helpful in sorting algorithms, mathematical operations, or simply reordering data.

We can achieve the swap in many ways in this blog .We will using two different way to achieve the concept swapping.

there are :-

1.USING THIRD VARIABLE.
2.WITHOUT USING THIRD VARIABLE.
3.GETTING RUN TIME INPUT.

1.USING THIRD VARIABLE

Fist create the variable declaration like ( a , b and c).Assign the value for a and b ,use the c as a temporary variable.


How its works ?

  1. First add the two assigned variable such as a and b .
  2. Then store the added value in c (third variable).
  3. assign the value for a is c-a. And assign the value for b is c-a.
       c=10+20;     c= 30;
       a=c-a;
       a=30-10;      a=20;
       b=c-a;
       b=30-20;      b=10;

package swappro;


static int a=10,b=20,c;

public static void main(String args[]) {


System.out.println("Before swapping\n");

System.out.println("the value of a :"+a+"the value of b :"+b);

c=a+b;

a=c-a;

b=c-a;

System.out.println("\nAfter swapping \n");

System.out.println("the value of a :" +a +"the value of b :"+b);

}

}



OUTPUT

Before swapping


the value of a :10the value of b :20


After swapping


the value of a :20the value of b :10

2. WITHOUT USING THIRD VARIABLE

Fist create the variable declaration like ( a and b).

How its works ?

  1. First add the two assigned variable such as a and b ..
  2. assign the value for a is a+b. And assign the value for b is a-b .Then re-assign the value for a is a-b.
      
  a=a+b;
       a=10+20;     a= 30;
  b=a-b;   
       b=30-20;      b=10;
  a=a-b;
       a=30-10;      a=20;

package swappro;



public class Swapiing {


static int a=10,b=20;

public static void main(String[] args) {

System.out.println("Before swapping\n");

System.out.println("the value of a :"+a+"the value of b :"+b);

a=a+b;

b=a-b;

a=a-b;

System.out.println("\nAfter swapping \n");

System.out.println("the value of a :"+a+"the value of b :"+b);

}


}


OUTPUT

Before swapping


the value of a :10the value of b :20


After swapping


the value of a :20the value of b :10

3.GET THE RUN TIME INPUTS


We use the same method  .which is  using third variable ,just get the input at the run time by using Scanner class.

package swappro;


import java.util.Scanner;


public class Swapiing {


static int a=10,b=20,c;

public static void main(String[] args) {


Scanner ss=new Scanner(System.in);

System.out.println("ENTER THE VALUE OF a :");

a=ss.nextInt();

System.out.println("ENTER THE VALUE OF b :");

b=ss.nextInt();

System.out.println("Before swapping\n");

System.out.println("the value of a :"+a+"the value of b :"+b);

System.out.println("\nAfter swaping \n");

c=a+b;

a=c-a;

b=c-b;

System.out.println("\nAfter swapping \n");

System.out.println("the value of a :"+a+"the value of b :"+b);

}


}

OUTPUT

ENTER THE VALUE OF a :

20

ENTER THE VALUE OF b :

30

Before swapping


the value of a :20 the value of b :30


After swapping


the value of a :30 the value of b :20

.

No comments:

Post a Comment

DDL command in sql

DATA DEFINITION LANGUAGE DBMS language database language are used to read,update and store data in a database.There are several suc...