Java Program

Object:-Write a Program in java to swap two number.

class Swap
{
 public static void main(String…s)
 {
  int a,b,temp;
  a=Integer.parseInt(s[0]);
  b=Integer.parseInt(s[1]);
  System.out.println(“nBefore Swap:n”+”a=”+a+”tb=”+b);
  temp=a;
  a=b;
  b=temp;
  System.out.println(“n After Swap:n”+”a=”+a+”t b=”+b);
 }
}

Output

5 6

Before Swap: a=5 b=6
After Swap : a=6 b= 5
 

Explanation

Here we interchange the value of both the variable using third variable.








You may also like: