Skip to main content

write a program to swap two variables without a temp variable

import java.io.*;
import java.util.Scanner;
public class swaptwovariables
{
public static void main(String args[]) throws Exception
{
System.out.println("Welcome to the swaptwovariables project!");
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers a and b to swap");

int a= input.nextInt();
int b= input.nextInt();
System.out.println("a and b before swapping "+a+" "+b);
a=a+b;
b=a-b;
a=a-b;

System.out.println("a and b after swapping "+a+" "+b);
}

}

Comments