Suppose a program contains the two integer variables x and y

Suppose a program contains the two integer variables x and y. a) (5 points) First, a program segment to exchange x and y using a temporary vanable for storage b) 10 points Rewrite this program segment to x and y WITHO using a temporary variable

Solution

int a =5;

       int b=9;

      

       System.out.println(\"before exchange : \"+a+\" \"+b);

       //exchange with temporary variable

      

       int t=a;

       a=b;

       b=t;

      

       System.out.println(\"after 1st exchange : \"+a+\" \"+b);

      

       //exchange without temporary variable

      

       a = a+b;

       b = a-b;

       a= a-b;

       System.out.println(\"after 2nd exchange : \"+a+\" \"+b);

 Suppose a program contains the two integer variables x and y. a) (5 points) First, a program segment to exchange x and y using a temporary vanable for storage

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site