Saturday, November 15, 2014

Filled Under: , , , , , ,

Program to swap two values using third variable in java.

Share
Swapping using third variable


Code Box
--------------------------------------------

/*program to swap two values using third variable */

import java.io.*;

class swap
{
public static void main(String x[]) throws IOException
{
int a,b,temp;

BufferedReader obj=new BufferedReader(new InputStreamReader(System.in));

System.out.print("\nEnter two numbers :");
System.out.print("\nEnter first number a= ");
a=Integer.parseInt(obj.readLine());

System.out.print("\nEnter second number b= ");
b=Integer.parseInt(obj.readLine());

System.out.print("\nValues before swapping a= "+a+" & b= "+b);

temp=a;   //swapping
a=b;
b=temp;

System.out.print("\nValues after swapping a= "+a+" & b= "+b);

}
}



5 comments:

  1. Bro ur program is awesome it really helped me but can I pls. tell me why have I not used PRINTLN anywhere

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Thank you Ajay Nagarkar, for your valuable comment.

      And answer for your question why I didn't use println() and use print() is here

      http://codeboxjava.blogspot.in/2016/05/difference-between-println-and-print-in.html

      please go through it. I explained it for you. And if you get any problem please feel free to comment back.

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
    Replies
    1. Thank you Ajay Nagarkar, for your valuable comment.

      And answer for your question why I didn't use println() and use print() is here

      http://codeboxjava.blogspot.in/2016/05/difference-between-println-and-print-in.html

      please go through it. I explained it for you. And if you get any problem please feel free to comment back.

      Delete