Swap Two Numbers without Temp Variable in C


Q. Write a C program for Swap Two Numbers without using temp variable.

#include 
#include  

void main()
{
	int x, y;
	clrscr();
	printf("Enter two numbers: ");
	scanf("%d%d", &x, &y);
	printf("Before swapping(interchange) the numbers\nx= %d\ty= %d\n", x, y);
	x=x+y;
    y=x-y;
    x=x-y;
	printf("After swapping(interchange) the numbers\nx= %d\ty= %d", x, y);
	getch();
}

Q. Write an algorithm to Swap Two Numbers without using temp variable.

1. Start
2. Read two integers `x` and `y` from the user
3. Display the values of `x` and `y` before swapping
4. Perform swapping:
    - Set `x = x + y`
    - Set `y = x - y` (which is original `x`)
    - Set `x = x - y` (which is original `y`)
5. Display the values of `x` and `y` after swapping
6. End 



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.