[Updated] Goldman Sachs Aptitude Test Questions and Answers
Practice List of TCS Digital Coding Questions !!!
Take 50+ FREE!! Online Data Interpretation Mock test to crack any Exams.

Program Discussion :: Basics

Home > Programs > Basics

28 / 279

Write a Swapping program using XOR operation.

Answer:

import java.util.*;

public class SwapUsingXor
{
    public static void main(String []args)
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter 2 numbers: ");
        int a=sc.nextInt();
        int b=sc.nextInt();
        System.out.println("Before swapping a= "+a+" || b= "+b);
        a=a^b;
        b=a^b;
        a=a^b;
        System.out.println("After swapping a= "+a+" || b= "+b);
    }
}

Asked In :: Virtusa

Post Your Answer Here:

Language:

Post Your Reply Here: