-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSwapNumbers.java
More file actions
23 lines (17 loc) · 686 Bytes
/
SwapNumbers.java
File metadata and controls
23 lines (17 loc) · 686 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import java.util.Scanner;
public class SwapNumbers {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter the second number: ");
int num2 = scanner.nextInt();
System.out.println("Before swapping: num1 = " + num1 + ", num2 = " + num2);
// Swap the numbers without using a temporary variable
num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;
System.out.println("After swapping: num1 = " + num1 + ", num2 = " + num2);
scanner.close();
}
}