Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2017
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

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

  2. #2
    New Coder
    Join Date
    Jan 2016
    Location
    Malaysia
    Posts
    48
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 20th, 2017 at 02:44 AM.

  3. #3
    New to the CF scene
    Join Date
    Oct 2017
    Location
    D-58 Noida Sec 2 Near RED FM Lane
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 23rd, 2017 at 04:35 PM. Reason: added code tags; removed self-promotional link

  4. #4
    New to the CF scene
    Join Date
    Jul 2017
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 26th, 2017 at 02:21 PM. Reason: added code tags

  5. #5
    New to the CF scene
    Join Date
    Feb 2018
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;
    }


 

Tags for this Thread

Posting Permissions

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