Sunday, July 16, 2017

Write a program that swaps the value of two variables.

/**
 * Created by Jo on 7/10/2017.
 * For more exercises and lessons, visit http://shegertech.blogspot.com/
 * Chapter 4, Exercise 2
 */

import java.util.Scanner;

public class Q2Solution {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int num1 = 7, num2 = 3, temp = 0;
        System.out.print("# # # Values before swap # # #\n\n");
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

        temp = num1;
        num1 = num2;
        num2 = temp;

        System.out.print("\n\n# # # Values after swap # # #\n\n");
        System.out.println("num1 = " + num1);
        System.out.println("num2 = " + num2);

    }
}

Share: