name the function which is used to swap the values of two memory locations using third variable
Answers
Answered by
2
The idea is simple
Assign x to a temp variable : temp = x
Assign y to x : x = y
Assign temp to y : y = temp
Let us understand with an example.
x = 100, y = 200
After line 1: temp = x
temp = 100
After line 2: x = y
x = 200
After line 3 : y = temp
y = 100
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
Output:
Enter Value of x 12
Enter Value of y 14
After Swapping: x = 14, y = 12
Pls mark me brainiest
Assign x to a temp variable : temp = x
Assign y to x : x = y
Assign temp to y : y = temp
Let us understand with an example.
x = 100, y = 200
After line 1: temp = x
temp = 100
After line 2: x = y
x = 200
After line 3 : y = temp
y = 100
int main()
{
int x, y;
printf("Enter Value of x ");
scanf("%d", &x);
printf("\nEnter Value of y ");
scanf("%d", &y);
int temp = x;
x = y;
y = temp;
printf("\nAfter Swapping: x = %d, y = %d", x, y);
return 0;
}
Output:
Enter Value of x 12
Enter Value of y 14
After Swapping: x = 14, y = 12
Pls mark me brainiest
Similar questions
Science,
3 months ago
Math,
3 months ago
Biology,
6 months ago
English,
6 months ago
Political Science,
10 months ago