+ 6
If a=3 and b=2 how can you swap the two values without declaring a third variable in java or c#
code
8 Antworten
+ 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.
+ 5
int a = 3;
int b = 2;
a = a + b; // 5
b = a - b; // 3
a = a - b; // 2
+ 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.
+ 4
my java teacher gave me as a short assignment
+ 3
int a=3,b=2;
a=a-b; //1
b=a+b; //3
a=b-a; //2
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;
}
- 4
impossibry :)
- 4
Temporal variable is necessary.