Swap 2 numbers without using third variable
Algorithm
Input Two numbers a and b to be swapped Output Numbers after swapping complexity O(1). Swap(a , b) 1. a=a+b; 2. b=a-b; 3. a=a-b; 4. print a and b
Algorithm Description
This algorithm takes two integers and swaps their values without using temporary variable
C Code
//Swap two numbers without using temporary variable and pointers
//Input : Swapped numbers
#include< stdio.h>
#include< conio.h>
int main()
{
int a,b;
printf("Enter two numbers ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("No(s) after swapping are %d %d",a,b);
}