Java program to swap two number by taking input from user


Program
import java.util.*; public class Swapping { public static void main(String[] args) { Scanner s=new Scanner(System.in); System.out.println("Enter the value of a an b"); int a=s.nextInt(); int b=s.nextInt(); int temp; // Value of a is copied in temp temp = a; // Value of b is copied to a, this will overwrite a a = b; // finally value of temp is copied in b,this will overwrite b b = temp; System.out.println("After swapping"); System.out.println("a = " + a); System.out.println("b = " + b); } }
Input
Output