Swap Program with temporary variable using JAVA

WHAT IS SWAP:

  • Swapping is the process of changing the values of two variables. 
  • Able to change the values of two variables in different ways.



SWAP PROGRAM WITH TEMPORARY VARIABLE USING JAVA:

    Swap the values of two variables with the use of the temporary variable. 

    In the above image, we have two boxes. The first box has a red ball and the second box has a green ball. We have to exchange the balls in the two boxes. 

    Using a temporary box, we have to swap the balls in the boxes.


PROGRAM:

import java.lang.*;

class Swap
{
public static void main(String args[])
{
int a = 10, b=20, temp;

System.out.println("Before Swapping: a = " + a + " b = " + b);

temp = a;
a = b;
b = temp;

System.out.println("After Swapping: a = " + a + " b = " + b);

}
}

Output:




                                                    Thanks for choosing our blog 😊. 

Comments

Popular posts from this blog

MySQL Tutorial Part 1

Pandas DataFrame empty

Introduction to Java