+ 6

[DUPLICATE] Write a program in C/C++/Java/Python to swap the values(integral) of two variables without using third variable.

11th Apr 2018, 12:46 PM
Ajay Gupta
Ajay Gupta - avatar
18 Answers
0
Santosh Arya The goal was to swap the values in memory, not to print them pretending that they were swapped.
12th Apr 2018, 12:03 PM
Timon Paßlick
+ 21
https://code.sololearn.com/c19DZI3wop0D/?ref=app its for swap 3 variables without Auxiliary variable for 2 variable almost same to this
13th Apr 2018, 10:46 PM
AliR૯za
AliR૯za - avatar
17th Apr 2018, 8:14 PM
MeanMachine
MeanMachine - avatar
+ 17
Ace 👍 , i fix that
14th Apr 2018, 4:15 AM
AliR૯za
AliR૯za - avatar
13th Apr 2018, 5:46 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 15
https://code.sololearn.com/cfc26oWm4U0p/?ref=app
12th Apr 2018, 5:57 AM
LukArToDo
LukArToDo - avatar
+ 6
Python a = 10 b = 20 a, b = b, a print("a: {}\nb: {}".format(a, b))
11th Apr 2018, 1:04 PM
TurtleShell
TurtleShell - avatar
+ 5
int a=10; int b=20; asm ( "movl %2, %%eax;" "movl %3, %0;" "movl %%eax, %1;" :"=r"(a), "=r"(b) :"r"(a), "r"(b) :"%eax" ); printf("a: %d\nb: %d\n", a, b);
11th Apr 2018, 1:11 PM
Alex
Alex - avatar
+ 5
Solution in pyhton: a,b=b,a
12th Apr 2018, 6:21 AM
Mitali
Mitali - avatar
+ 4
The asm code of @Alex is actually faster than std::swap here in SL.
11th Apr 2018, 2:24 PM
Timon Paßlick
11th Apr 2018, 2:34 PM
Alex
Alex - avatar
+ 4
c a=a+b b=a-b a=a-b
11th Apr 2018, 3:40 PM
Jithesh Cj
Jithesh Cj - avatar
12th Apr 2018, 9:48 AM
Nitish kumar jha
Nitish kumar jha - avatar
+ 1
http://lmgtfy.com/?q=swap%20without%20third%20variable
12th Apr 2018, 6:42 AM
VcC
VcC - avatar
+ 1
#inculde<stdio.h> void main() { int a ,b; print("enter two number "); scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("after swapping a=%d \t b=%d",a,b); getch(); }
12th Apr 2018, 12:48 PM
Jithesh Cj
Jithesh Cj - avatar
- 1
In the language 'C': #include<stdio.h> #include<conio.h> void main() { int a,b; printf("Enter number A :"); scanf("%d",a); printf("Enter number B:"); scanf("%d",&b); printf("After swapping :"); printf("A=%d",b); printf("B=%d",a); getch(); }
12th Apr 2018, 10:13 AM
Santosh Arya
Santosh Arya - avatar