-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path017-Swap-Two-Numbers-Using-Third-Variable.java
More file actions
61 lines (51 loc) · 2.29 KB
/
Copy path017-Swap-Two-Numbers-Using-Third-Variable.java
File metadata and controls
61 lines (51 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* ============================================================================
* Program Name : Swap Two Numbers Using Third Variable
* File Name : 017-Swap-Two-Numbers-Using-Third-Variable.java
* Class Name : SwapTwoNumbersUsingThirdVariable
*
* Description:
* This program demonstrates how to swap the values of two variables
* using a third (temporary) variable. The temporary variable stores
* one value while the other value is exchanged.
*
* Algorithm:
* Step 1: Store the value of the first variable in a temporary variable.
* Step 2: Assign the value of the second variable to the first variable.
* Step 3: Assign the value stored in the temporary variable to the second variable.
*
* Objective:
* - Understand the concept of swapping two variables.
* - Learn how to use a temporary variable for swapping.
* - Display the values before and after swapping.
*
* Author : Shaik Mahaboob Basha
* Repository : 01-Core-Java
* Folder : 08-Java-Programs
* ============================================================================
*/
public class SwapTwoNumbersUsingThirdVariable {
// The main() method is the entry point of every Java application.
public static void main(String[] args) {
// Declare and initialize the first number.
int firstNumber = 25;
// Declare and initialize the second number.
int secondNumber = 50;
// Display the values before swapping.
System.out.println("Before Swapping:");
System.out.println("First Number : " + firstNumber); // Output: 25
System.out.println("Second Number : " + secondNumber); // Output: 50
// Declare a temporary variable to store the first number.
int temporary;
// Store the value of the first number in the temporary variable.
temporary = firstNumber;
// Assign the value of the second number to the first number.
firstNumber = secondNumber;
// Assign the value stored in the temporary variable to the second number.
secondNumber = temporary;
// Display the values after swapping.
System.out.println("\nAfter Swapping:");
System.out.println("First Number : " + firstNumber); // Output: 50
System.out.println("Second Number : " + secondNumber); // Output: 25
}
}