In this post we will explain two methods to swap the value of two variables.
Method 2: swapping two numbers without a temp variable.
We can swap two variables using a third or temporary variable with the use of
three assignment statements.
three assignment statements.
The basic technique is to store in the temp variable the value of one of the two
variables to swap. If the two variables are A and B, we must follow this steps to swap them:
variables to swap. If the two variables are A and B, we must follow this steps to swap them:
Step 1: The value of the variable A is stored in the temp variable
temp = A;
Step 2: Once the value of A is stored, we can store in A the value of B
A = B;
Step 3: And finally the initial value of A (temp) is stored in B:
B = temp;
For example,
A = 1
B = 2
The logic to swap two variables using a third variable is the same for all the
programming languages. It also can be done by first storing the value of B.
programming languages. It also can be done by first storing the value of B.
temp = B;
B = A;
A = temp;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A, B, temp;
System.out.print("Enter an integer value for A: ");
A = input.nextInt();
System.out.print("Enter an integer value for B: ");
B = input.nextInt();
System.out.println("Before: A = " + A + " B = " + B);
temp = A;
A = B;
B = temp;
System.out.println("After: A = " + A + " B = " + B);
}
}
Output:
Enter an integer value for A: 1
Enter an integer value for B: 2
Before: A = 1 B = 2
After: A = 2 B = 1
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A, B, temp;
System.out.print("Enter an integer value for A: ");
A = input.nextInt();
System.out.print("Enter an integer value for B: ");
B = input.nextInt();
System.out.println("Before: A = " + A + " B = " + B);
temp = A;
A = B;
B = temp;
System.out.println("After: A = " + A + " B = " + B);
}
}
Output:
Enter an integer value for A: 1
Enter an integer value for B: 2
Before: A = 1 B = 2
After: A = 2 B = 1
2. Java program to swap two numbers without using a temp variable.
We can do it by performing arithmetic operations.
If the two variables are A and B, we can perform one addition followed by two
subtractions:
subtractions:
A = A + B;
B = A – B;
A = A – B;
For example,
A = 2
B = 3
We can also do it by performing one multiplication followed by two divisions:
A = A * B;
B = A / B;
A = A / B;
Here is the complete Java code using the addition-subtraction method:
import java.util.Scanner;
public class JavaApplication360 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A, B;
System.out.print("Enter an integer value for A: ");
A = input.nextInt();
System.out.print("Enter an integer value for B: ");
B = input.nextInt();
System.out.println("Before: A = " + A + " B = " + B);
A = A + B;
B = A - B;
A = A - B;
System.out.println("After: A = " + A + " B = " + B);
}
}
Enter an integer value for A: 2
Enter an integer value for B: 3
Before: A = 2 B = 3
After: A = 3 B = 2
No comments:
Post a Comment