Java Program to swap two numbers
In this example, we are giving a program to swap two numbers.
To swap two numbers:
# To swap two numbers
public class SwapNumbers {
public static void main(String[] args) {
float first = 7.5f, second = 3.9f;
System.out.println("Before swapping:");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
float temporary = first;
first = second;
second = temporary;
System.out.println("After swapping:");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Output:
Before swapping: First number = 7.5 Second number = 3.9 After swapping: First number = 3.9 Second number = 7.5