C Program to Swap Two Numbers without Using Third Variable

In this source code example, we will write a C program for swapping the values of two variables without using a third variable.

C Program to Swap Two Numbers Without Using Third Variable

In this C program, we will take input from the User or console and print the result to the output:  
/*Program to accept two numbers & swap the values*/
#include<stdio.h>
main() {
	/*Declare the variables*/
	int num1, num2;
	printf("Enter first number: ");
	scanf("%d", &num1);
	printf("Enter second number: ");
	scanf("%d", &num2);
	printf("Numbers before swapping: %d %d", num1, num2);
	/*swapping the values of variables*/
	num1 = num1 + num2;
	num2 = num1 - num2;
	num1 = num1 - num2;
	printf("\nNumbers after swapping: %d %d", num1, num2);
}

Output:

Enter first number: 10
Enter second number: 20
Numbers before swapping: 10 20
Numbers after swapping: 20 10





Comments