Swap Two Numbers in Java Using FunctionIn this section, we will create Java programs to swap two numbers using functions with different logic.
Java Program to Swap Two Numbers Using FunctionUsing User-defined FunctionSwapNumbers.java Output: Enter the first number: 25 Enter the second number: 12 After swapping: a= 12, b= 25 Swap Two Numbers Without Using Third VariableSwapNumber.java Output: Enter the first number: 78 Enter the second number: 45 Before Swapping x = 78 y = 45 After Swapping x = 45 y = 78 In the above program, we can replace the swapping logic with the following logic: Swap Two Numbers Using Bitwise XOR (^) OperatorSwapNumberExample.java Output: Before swapping values of x and y are: 12, 18 After swapping values of x and y are: 18, 12 Let's see another logic to swap two numbers. SwapNumbersExample.java Output: Before swapping, values of x and y are: 12, 18 After swapping: x = 18, y = 12
Next TopicCullen Number in Java
|