Web Analytics Made Easy -
StatCounter How to Swap two numbers without using third variable in Java? - CodingForum

Announcement

Collapse
No announcement yet.

How to Swap two numbers without using third variable in Java?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • deathshadow
    replied
    Oh, and just for laughs, those of you who say you can't do this with strings? (which I've heard in the past)

    Code:
    public class SwapTwoStrings
    {
    
        public static void main(String[] args)
        {
            String first = "bingo";
            String second = "joker";
            System.out.println("Before Swapping Two Strings : ");
            System.out.println("First String = " + first);
            System.out.println("Second String = " + second);
            first = second + (second = first);
            first = first.substring(0, first.length() - second.length());
            System.out.println("After Swapping Two Strings : ");
            System.out.println("First String = " + first);
            System.out.println("Second String = " + second);
        }
    }
    Not bad considering I've not touched Java in a decade and a half.

    Leave a comment:


  • deathshadow
    replied
    Fun bump of a three year old thread... I wonder, does Java support operations on assignment like all other C syntax languages? I can't even test this since Java is a "yeah, no" for me. Will NOT even install it anymore.

    Code:
    x = y - x + (y = x);
    May work. Yes, SINGLE = inside the assignment. Works in JS, PHP, and C... it's one of the oldest tricks out there.

    ... and people wonder why I say they overthink problems.

    -- edit --

    tested it on tutorialspoint's online compiler, this works:

    Code:
    public class SwapTwoNumbers
    {
    
        public static void main(String[] args)
        {
            int first = 10;
            int  second = 20;
            System.out.println("Before Swapping Two Numbers : ");
            System.out.println("First Number = " + first);
            System.out.println("Second Number = " + second);
            first = second - first + (second = first);
            System.out.println("After Swapping Two Numbers : ");
            System.out.println("First Number = " + first);
            System.out.println("Second Number = " + second);
        }
    }
    REMEMBER when you do an assignment (single =) in C syntax languages, you can also operate on the result as it is returned. If it didn't, we wouldn't need the difference between var++ and ++var. It's also why the people who say "if you see a single = in a condition, the code must be wrong" are talking out their backsides.
    Last edited by deathshadow; Jul 29, 2020, 01:01 AM.

    Leave a comment:


  • Ishan Shah
    replied
    Another way is the following for Swap two numbers without using third variable in Java:

    Code:
    public class SwapTwoNumbers 
    {
    
    	public static void main(String[] args) 
    	{
    		int first = 10;
    		int  second = 20;
    		System.out.println("Before Swapping Two Numbers : ");
    		System.out.println("First Number = " + first);
                    System.out.println("Second Number = " + second);
    		first = first - second;
                    second = first + second;
    	        first = second - first;
                    System.out.println("After Swapping Two Numbers : ");
                    System.out.println("First Number = " + first);
                    System.out.println("Second Number = " + second);
    	}
    }

    Leave a comment:


  • Mridula
    replied
    Another way of swapping two numbers without using the third number is by using a bitwise X-OR operator.
    #include <stdio.h>
    int main()
    {
    int x = 10, y = 5;

    x = x ^ y;
    y = x ^ y;
    x = x ^ y;
    printf("After Swapping: x = %d, y = %d", x, y);
    return 0;
    }

    Leave a comment:


  • plague
    replied
    Code:
    public class Swap(){
    public static void main(String args[]){
    int a=10;
    int b=20;
    System.out.println("Before swapping-the value of a is "+a" and the value of b is +b");
    a=a*b;
    b=a/b;
    a=a/b;
    System.out.println("After swapping-the value of a is "+a" and the value of b is +b");
    }
    }
    Last edited by vinyl-junkie; Oct 26, 2017, 09:21 AM. Reason: added code tags

    Leave a comment:


  • shujat2
    replied
    Code:
    public class Swapping
    {
     
        public static void main(String a[])
    {
            int a = 10;
            int b = 20;
            System.out.println("Before swap:");
            System.out.println("A value: "+a);
            System.out.println("B value: "+b);
            a = a+b;
            b=a-b;
            a=a-b;
            System.out.println("After swap:");
            System.out.println("A value: "+a);
            System.out.println("B value: "+b);
        }
    }
    Last edited by vinyl-junkie; Oct 23, 2017, 11:35 AM. Reason: added code tags; removed self-promotional link

    Leave a comment:


  • john joe
    replied
    You can get many example from google

    Below is the one

    Code:
    package com.java2novice.algos;
     
    public class MySwapingTwoNumbers {
     
        public static void main(String a[]){
            int x = 10;
            int y = 20;
            System.out.println("Before swap:");
            System.out.println("x value: "+x);
            System.out.println("y value: "+y);
            x = x+y;
            y=x-y;
            x=x-y;
            System.out.println("After swap:");
            System.out.println("x value: "+x);
            System.out.println("y value: "+y);
        }
    }
    Hope this help you
    Last edited by john joe; Oct 19, 2017, 09:44 PM.

    Leave a comment:


  • How to Swap two numbers without using third variable in Java?

    How to Swap two numbers without using third variable in Java?
Working...
X