Java Program for Swap Numbers Without Using Third Variable.

public class SwapElementsWithoutThirdVariableExample {
 
        public static void main(String[] args) {
               
                int num1 = 10;
                int num2 = 20;
               
                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("Before Swapping");
                System.out.println("Value of num1 is :" + num1);
                System.out.println("Value of num2 is :" +num2);
        }
 
 
}
Java Program for Swap Numbers Without Using Third Variable. Java Program for Swap Numbers Without Using Third Variable. Reviewed by on December 21, 2019 Rating: 5

No comments:

Powered by Blogger.