40. Write a program in Java to interchange the value (swap) of the two numbers a and b and display
the result after swapping.
Use overloaded methods display (int, int) and display (float, float) for swapping integer and float
type values.
Answers
Answered by
1
Explanation:
public class SwapNumbers {
public static void main(String[] args) {
float first = 1.20f, second = 2.45f;
System.out.println("--Before swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
// Value of first is assigned to temporary
float temporary = first;
// Value of second is assigned to first
first = second;
// Value of temporary (which contains the initial value of first) is assigned to second
second = temporary;
System.out.println("--After swap--");
System.out.println("First number = " + first);
System.out.println("Second number = " + second);
}
}
Similar questions