Results 1 to 3 of 3

Thread: Swap Any Two Numbers in JAVA

  1. #1
    EvansMitchell is offline Senior Member
    Join Date
    Dec 2009
    Posts
    227
    Rep Power
    3

    Default Swap Any Two Numbers in JAVA

    In this code we will observe how we can swap two numbers. We can perform this by using a temporary variable which is used to store variable so that we can easily swap the numbers.

    Code:
    public class Swapping{
    static void swap(int a,int b){
    int temp=a;
    a=b;
    b=temp;
    System.out.println("After swapping a = " + a + " b = " + b);
    }
    public static void main(String[] args){
    int a=1;
    int b=2;
    
    System.out.prinln("Before swapping a="+a+" b="+b);
    swap(a, b);
    }
    }

  2. #2
    RogersNguyen is offline Senior Member
    Join Date
    Dec 2009
    Posts
    227
    Rep Power
    3

    Default

    Try out this code, when you run this program you swap number as an output result:

    Code:
    public class Swap {
      public static void main(String[] args) {
      
        int x= Integer.parseInt(args[0]);
        int y= Integer.parseInt(args[1]);
        System.out.println("Before Swapping: "+ x +" and " + y);
        int z = x;
        x = y;
        y = z;
        System.out.println("After swapping: " + x +" and " + y);
      }
    }

  3. #3
    TorresScott is offline Senior Member
    Join Date
    Dec 2009
    Posts
    239
    Rep Power
    3

    Default

    It is very simple code.So just have a look at the coding which can make you to understand more properly

    Code:
    public class swapnumber {
    static void insertvalue (int x,int y )
    {
    int c;
    c=x;
    x=y;
    y=c;
    }
    public static void main(String[] args) {
    String o;
    int a;
    o=JOptionPane.showInputDialog("insert a");
    a=Integer.parseInt(o);
    String l;
    int b;
    l=JOptionPane.showInputDialog("insert b");
    b=Integer.parseInt(l);
    insertvalue(a,b) ;
    System.out.println("x="+a);
    System.out.println("y="+b);

Similar Threads

  1. Swap two numbers without using temp variable
    By BellWard in forum Programming
    Replies: 1
    Last Post: 07-09-2010, 02:16 PM
  2. Swap HDDs, will windows boot in a different PC?
    By ThomasBarnes in forum Operating System
    Replies: 2
    Last Post: 06-23-2010, 01:36 PM
  3. Comparing Two Numbers in java
    By RogersNguyen in forum Programming
    Replies: 2
    Last Post: 05-13-2010, 01:46 PM
  4. SNIA standardize swap of data
    By Fabian Nadal in forum Latest Hardware News
    Replies: 0
    Last Post: 04-14-2010, 11:33 AM
  5. IE 7 can swap between the open Tabs
    By leshter in forum General Internet Terms
    Replies: 0
    Last Post: 01-31-2009, 10:12 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
SEO by SubmitEdge

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48