Google+ Followers

Connect

Get Updates Via Email

Enter your email address:

Delivered by FeedBurner

Sunday, 10 November 2013

Java Program: Swap two numbers without temp variable

import java.io.*;
public class Swapwithouttemp {
    public static void main(String[] args) throws IOException
    {
        System.out.println("Enter first number");
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int x=Integer.parseInt(br.readLine());    
        System.out.println("Enter second number");
        BufferedReader brr=new BufferedReader(new InputStreamReader(System.in));
        int y=Integer.parseInt(brr.readLine());        
        new swap(x,y);
    }
}
class swap
{  
    swap(int a, int b) throws IOException          
    {
        System.out.println("Before swapped values a= "+a+"\tb= "+b );
        a=a+b;
        b=a-b;
        a=a-b;
        System.out.println("After swapped values a= "+a+"\tb= "+b );
    }
}

1 comment :

  1. I have read your blog its very attractive and impressive. I like it your blog.

    Java Online Training Java EE Online Training Java EE Online Training Java 8 online training Core Java 8 online training

    Java Online Training from India Java Online Training from India Core Java Training Online Core Java Training Online Java Training InstitutesJava Training Institutes

    ReplyDelete

Share this to your friends