PROGRAM FOR SWAPPING OF TWO NUMBERS

A typical value swapping progran involves use of a temporay storage to store the value of either of the two variables whose values is to be swapped.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    package pack;
    import java.io.*;
    public class Swap
    {
        public static void main(String[] args) throws IOException
        {
            System.out.print("Enter value for A = ");
            InputStreamReader a = new InputStreamReader(System.in);
            BufferedReader x = new BufferedReader(a);
            String str = x.readLine();
            int i = Integer.parseInt(str);
            System.out.print("Enter value for B = ");
            InputStreamReader b = new InputStreamReader(System.in);
            BufferedReader y = new BufferedReader(b);
            String Str1 = y.readLine();
            int j = Integer.parseInt(Str1);
            System.out.println("After swaping the Result is : ");
            i=i+j;
            j=i-j;
            i=i-j;
            System.out.println("A = " +i);
            System.out.print("B = " +j);
        }
    }

    Output :Swapping of Two Numbers

    IPpyramid1