Swap numbers without using third variable in C
scanf() - Question 10
In this question, we will see how to input two integers into two variables x and y in C programming using the scanf() function and swap their values without using a third variable. To know more about scanf() function click on the scanf() function lesson.
Q10) Write a program in C to input two integers into two variables x and y and swap their values without using a third variable.
x = 5
y = 3
After Swap
x = 3
y = 5
Program
#include <stdio.h>
#include <conio.h>
int main()
{
int x,y;
printf("Enter value of x ");
scanf("%d",&x);
printf("Enter value of y ");
scanf("%d",&y);
x=x+y;
y=x-y;
x=x-y;
printf("After Swap\n");
printf("x=%d\n",x);
printf("y=%d",y);
return 0;
}
Output
Enter value of x 10 Enter value of y 20 After Swap x=20 y=10