Java Program To Swap The Values Of Two Variables Without Using Third Variable - For Beginners

Pubblicato il: 04 novembre 2017
sul canale di: Computer Analyst
57
2

Code :
import java.util.*;

public class Swap{
public static void main(String args[]){
int a, b;
Scanner sc = new Scanner(System.in);
System.out.println("Enter two values for swapping :");
a = sc.nextInt();
b = sc.nextInt();
System.out.println("The values of a & b before swapping : a = "+a+" b = "+b);
// main part of the code..
a = a + b;
b = a - b;
a = a - b;
System.out.println("The values of a & b after swapping : a = "+a+" b = "+b);
sc.close();
}
}

/* Explanation : Suppose a = 1 and b = 2 -> before swapping
Now, a = a + b = 1 + 2 = 3 => a = 3
b = a - b = 3 - 2 = 1 => b = 1
a = a - b = 3 - 1 = 2 => a = 2

a = 2 and b = 1 after swapping..
*/
-------------------------------------------------------------------------------

Thanks for watching..

Plz do subscribe, like and share..

For any query, plz comment in the comment section below..

Use Headphone or Earphone for better hearing of audio..
--------------------------------------------------------------------------------------

Video Link To Set Path For Java in Windows :
   • How To Set Path For Java in Windows - For ...  

-------------------------------------------------

Follow On :
✅Instagram :   / computer_analyst.yt  
✅Google+ : https://plus.google.com/b/11676538144...
✅Twitter :   / yt_c_analyst  


In questa pagina del sito puoi guardare il video online Java Program To Swap The Values Of Two Variables Without Using Third Variable - For Beginners della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Computer Analyst 04 novembre 2017, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 57 volte e gli è piaciuto 2 spettatori. Buona visione!