store two integer values swap the values display the values before and after swapping in java
Answers
Answer:
Java program is your answer
Answer:
The variables are printed before swapping using println() to see the results clearly after swapping is done.
First, the value of first is stored in variable temporary (temporary = 1.20f).
Then, value of second is stored in first (first = 2.45f).
And, finally value of temporary is stored in second (second = 1.20f).
This completes the swapping process and the variables are printed on the screen.
Remember, the only use of temporary is to hold the value of first before swapping. You can also swap the numbers without using temporary.
Example 2: Swap two numbers without using temporary variable
public class SwapNumbers {
public static void main(String[] args) {
float first = 12.0f, second = 24.5f;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
first = first - second;
second = first + second;
first = second - first;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Output:
--Before swap--
First number = 12.0
Second number = 24.5
--After swap--
First number = 24.5
Second number = 12.0