Java Programs Swap Two Numbers Using User Input

Java Programs Swap Two Numbers Using User Input

import java.util.Scanner;

public class SwapNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.print(“Enter value of a: “);
int a = sc.nextInt();

System.out.print(“Enter value of b: “);
int b = sc.nextInt();

a = a + b;
b = a – b;
a = a – b;

System.out.println(“After swapping:”);
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
}
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *