In this C program, we will learn how to write a program to swap two numbers using + & - operator.
Here is the code of the program to swap two numbers using + & - operator.
//C Program to Swap Two Number using + & - operator
#include <stdio.h>
void main()
{
int a = 5, b = 10, temp;
printf("Number Before swap a= %d and b= %d", a, b);
//swapping Logic
a = a + b; //Here, a=15 (5+10)
b = a - b; //Here, b=5 (15-10)
a = a - b; //Here, a=10 (15-5)
printf("\nNumber After swapping a= %d and b= %d", a, b);
}
Number Before swap a= 5 and b= 10
Number After swapping a= 10 and b= 5
--------------------------------
Process exited after 0.04324 seconds with return value 37
Press any key to continue . . .
Comments