+ 6
[DUPLICATE] Write a program in C/C++/Java/Python to swap the values(integral) of two variables without using third variable.
18 Answers
0
Santosh Arya The goal was to swap the values in memory, not to print them pretending that they were swapped.
+ 21
https://code.sololearn.com/c19DZI3wop0D/?ref=app
its for swap 3 variables without Auxiliary variable
for 2 variable almost same to this
+ 17
Ace
👍 , i fix that
+ 15
https://code.sololearn.com/cfc26oWm4U0p/?ref=app
+ 6
Python
a = 10
b = 20
a, b = b, a
print("a: {}\nb: {}".format(a, b))
+ 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);
+ 5
Solution in pyhton:
a,b=b,a
+ 4
The asm code of @Alex is actually faster than std::swap here in SL.
+ 4
c
a=a+b
b=a-b
a=a-b
+ 1
http://lmgtfy.com/?q=swap%20without%20third%20variable
+ 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();
}
- 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();
}