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

Publicado el: 04 noviembre 2017
en el canal de: 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  


En esta página del sitio puede ver el video en línea Java Program To Swap The Values Of Two Variables Without Using Third Variable - For Beginners de Duración hora minuto segunda en buena calidad , que subió el usuario Computer Analyst 04 noviembre 2017, comparta el enlace con amigos y conocidos, en youtube este video ya ha sido visto 57 veces y le gustó 2 a los espectadores. Disfruta viendo!