Signed in as:
filler@godaddy.com
Signed in as:
filler@godaddy.com
/* swap A and B without a third var. */
/* Online C Compiler and Editor */
#include <stdio.h>
void swap( int a, int b)
{
printf("Before the swap A is : %d and B is %d \n", a, b);
// Store the sum of a + b in 'a''
a = a + b;
// Store the Difference of a - b in 'b'
b = a - b;
// Difference of sum and new 'b' is stored in 'a'
a = a - b;
printf("After the swap A is : %d and B is : %d \n", a, b);
}
int main()
{
int a = 10;
int b = 20;
swap(a, b);
return 0;
}
Result:
Before the swap A is : 10 and B is 20
After the swap A is : 20 and B is : 10
Copyright © 2023 Becker Engineering Technologies, LLC - All Rights Reserved.