Swap Two Numbers Using Third Temporary Variable | Java Programs

Output:

*** Swap Two Numbers Using Third Variable ***

*** Before swapping ***
a = 15
b = 27
*** After swapping ***
a = 27
b = 15

Click Here For Java Online Compiler

Solution:

public class SwapTwoNumbers1 {

    public static void main(String[] args) {

        int a, b, temp;
        a = 15;
        b = 27;
        System.out.println("*** Swap Two Numbers Using Third Variable ***" + "\n");
        System.out.println("*** Before swapping ***");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
        temp = a;
        a = b;
        b = temp;
        System.out.println("*** After swapping ***");
        System.out.println("a = " + a);
        System.out.println("b = " + b);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon