Gubbas Digi Communications

Gubbas Digi Communications

Swapping Numbers


Swapping Number

 

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

temp=a;
a=b;
b=temp;
System.out.println("The swapped value of a="+a+" b="+b);

}
}
Output:
 The swapped value of a=15 b=10

Swapping of two variables without using third variable

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

a=a*b;
b=a/b;
a=a/b;
System.out.println("The swapped values a="+a);
System.out.println(b);

}
}

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

a=a+b;
b=a-b;
a=a-b;
System.out.println("The swapped values a="+a);
System.out.println("b="+b);

}
}

Output:
    The swapped values a=15
     b=10




No comments:

Post a Comment