Using a TEMPORARY variable:
int a = 10, b = 20;
int temp = 0;
temp = a;
a = b;
b = temp;
Without using a temporary variable:
int a = 10, b = 20;
a = a + b; // ( a = 30 )
b = a - b; // ( b = 10 )
a = a - b; // ( a = 20 )
In one line:
int a = 10, b = 20;
a = a + b - ( b = a );
No comments:
Post a Comment