+ 6

If a=3 and b=2 how can you swap the two values without declaring a third variable in java or c#

code

5th Mar 2017, 9:41 PM
Karuma Tendai
Karuma Tendai - avatar
8 ответов
+ 8
In Java not with primitive types, and it's not the case of the easy a, b = b, a .... assigning. Create a function that does that.
5th Mar 2017, 10:23 PM
Mark Foxx
Mark Foxx - avatar
+ 5
int a = 3; int b = 2; a = a + b; // 5 b = a - b; // 3 a = a - b; // 2
6th Mar 2017, 12:24 PM
Саша Савчук
Саша Савчук - avatar
+ 4
Ah... actually, it might be possible. I don't think, however, this is what you expected nor useful than using a temporal variable. int a = 3; int b = 2; a = a * 10 + b; // multiplier can be changed b = a / 10; a %= 10; This agly code exactly uses only two variables. that's all.
5th Mar 2017, 10:34 PM
Twelfty
Twelfty - avatar
+ 4
my java teacher gave me as a short assignment
6th Mar 2017, 4:51 AM
Karuma Tendai
Karuma Tendai - avatar
+ 3
int a=3,b=2; a=a-b; //1 b=a+b; //3 a=b-a; //2
6th Mar 2017, 3:15 PM
Shubhendra Nath Singh
0
This is veryyy easy... the code is quite perfect. #include<stdio.h> int main() { int a,b; scanf("%d%d",&a,&b); a=a+b; b=a-b; a=a-b; printf("The answer:%d%d",a,b); return 0; }
16th Apr 2017, 6:53 AM
Hardik Khetan
Hardik Khetan - avatar
- 4
impossibry :)
5th Mar 2017, 10:21 PM
Андрій Дем'яненко
Андрій Дем'яненко - avatar
- 4
Temporal variable is necessary.
5th Mar 2017, 10:25 PM
Twelfty
Twelfty - avatar