All submissions

Pointer- Swap Value

Code

#include <stdio.h>

void swap(int *p1, int *p2) 
{
/* Way1: (time optimized,as less number of operations.But it consumes additional memory.)
    int temp=*p1;
    *p1=*p2;
    *p2=temp;
*/

/*Way2: (space optimized but time consuming because more operations to be performed.)
*p1=*p1+*p2;
*p1=*p1-*p2;
*p1=*p1-*p2;
*/

//Way3: Using Bitwise.
*p1=*p1 ^ *p2;
*p2=*p1 ^ *p2;
*p1=*p1 ^ *p2;

}

int main() 
{
    int a, b;
    scanf("%d %d", &a, &b);

    swap(&a, &b);

    printf("a = %d ", a);
    printf("b = %d", b);

    return 0;
}

Solving Approach

 

 

 

Loading...

Input

10 20

Expected Output

a = 20 b = 10