Swap Two Variables

Lab Java Beginner 10 points

Read two integers a and b. Swap them using a temporary variable and print a=… b=….

Input

One line: a b.

Output

a=B b=A

Example

Input

3 8

Output

a=8 b=3
Sample input available Sample output available
Editor
Java

Hints

Save a in temp before overwriting it.

Solution explanation

Without the temporary variable, the first assignment would lose the original value of a.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int temp = a;
        a = b;
        b = temp;
        System.out.println("a=" + a + " b=" + b);
    }
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD