Skip to main content

Java Program To Swap Two Numbers | 5 Ways To Swap The Numbers

Swap 2 Numbers | 5 Ways To Swap Two Numbers In Java

Way -1
==========

public class swap2Numbers
{
     public static void main(String[] args) {
        int a = 10, b =20;
        System.out.println("Before Swapping:" + a + "..." + b);

        // using third variable approach
        int temp =a;
        a=b;
        b=temp;
        System.out.println("After swapping:" + a +"..." + b);

     }

}

Way -2
============

public class swap2Numbers2 {

    // without using third variable approach

    public static void main(String[] args) {
        int a = 10, b = 20;
        System.out.println("Before Swapping:" + a + "..." + b);
        a = a + b;
        b = a - b;
        a = a - b;

        System.out.println("After Swapping:" + a + "..." + b);

    }

}

Way -3
============

public class swap2Numbers3 {

    // using multiplication and division operator approach
    public static void main(String[] args) {

        int a = 10, b = 20;

        System.out.println("Before Swapping:" + a + "..." + b);
        a = a * b;
        b = a / b;
        a = a / b;

        System.out.println("After Swapping:" + a + "..." + b);

    }

}

Way -4
================

public class swap2Numbers4 {

    public static void main(String[] args) {

        // using Bit-wise X-OR operator
        int a = 10 // Binary Representation 1010, b = 20 // Binary Representation 10100;
        /*
         *  a^b
         *    01010
         *   ^10100
         * =============
         *    11110
         * Like this way,
         *
         */
        System.out.println("Before Swapping:" + a + "..." + b);
        a = a ^ b;
        b = a ^ b;
        a = a ^ b;

        System.out.println("After Swapping:" + a + "..." + b);

    }

}

Way -5
==============

public class swap2Numbers5 {

    public static void main(String[] args) {
        int a = 10, b = 20;
        // with single statement approach
        System.out.println("Before Swapping:" + a + "..." + b);
        b = a + b - (a = b);
        // This expression execution starts from Right to left
        System.out.println("After swapping:" + a + "..." + b);

    }

}

Comments

Popular posts from this blog

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

Java 8 Coding Interview Questions | Asked In Many Interviews

 Q 1) Given A List . Write Java 8 Program to find Out 1st Element From List? ===================================================================== import java . util . Arrays ; import java . util . List ; public class FirstElementOfList {     // Given a List. Find First Element From the List       public static void main ( String [] args ) {                     List < Integer > list = Arrays . asList ( 10 , 15 , 24 , 7 , 24 , 74 , 10 , 7 );             list . stream (). findFirst (). ifPresent ( System . out :: println );     } } Q 2) Given A List ? Find Out the Count of All Element In A List? ============================================================================== import java . util . Arrays ; import java . util . List ; public class TotalNumberOfElements {     public static void main ( String [] args ) {       ...