SWAP VARIABLES WITHOUT USING THIRD VARIABLE EXAMPLE USING JAVA

public class Main {

    /**
     * @param args the command line arguments
     */
   public static void main(String[] args) {

                int num1 = 40;
                int num2 = 80;

                System.out.println("Before Swapping");
                System.out.println("Value of num1 is :" + num1);
                System.out.println("Value of num2 is :" +num2);

                //add both the numbers and assign it to first
                num1 = num1 + num2;
                num2 = num1 - num2;
                num1 = num1 - num2;

                System.out.println("After Swapping");
                System.out.println("Value of num1 is :" + num1);
                System.out.println("Value of num2 is :" +num2);
        }


}

output:
Before Swapping
Value of num1 is :20
Value of num2 is :80
After Swapping
Value of num1 is :80
Value of num2 is :40

No comments:

Post a Comment

how to call ssh from vs code

 To call SSH from VS Code, you can use the built-in Remote Development extension. This extension allows you to open a remote folder or works...