+ 2
Swapping of two numbers using functions
7 ответов
+ 7
Using function in C, I do not know yet.
Without using 3rd var
a=2 b=3
a+=b
b=a-b
a=a-b
+ 6
>> Using pointers
swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = *a;
}
int main() {
int a, b;
cin>>a>>b;
swap(&a,&b);
return 0;
}
Or use XOR operator.
a = a ^ b;
b = a ^ b;
a = a ^ b;
+ 1
swapping without using third variable by returning values in c#
function swap(x, y)
{
x, y=y, x
return x, y
}
or in c++
without returning values
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
cout<<a;
cout<<b;
}
+ 1
Void main()
{ clrscr ();
int a, b,temp;
cin>>a>>b;
temp=a;
a=b;
b=temp;
Cout<<a<<b;
getch();
}
0
Thanks my frinds
0
In java
public void main (int a, int b){
int temp;
temp = x;
x = y;
y = temp;
}
- 1
function swap (a,b){
a,b = b,a
Return a,b
}