Pointer- Swap Value

Code

#include <stdio.h>

void swap(int *p1, int *p2)
 {
    *p1=*p1+*p2;
    *p2=*p1-*p2;
    *p1=*p1-*p2;
    // Your logic here
}

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

First i declare 2 integer variables a and b  then i use the scanf function to read the values from user  after i call the swap functions i pass the arguments &a and &b and swap function stores the address in *p1 and *p2   then i use arithmetic  operators + and - through address i swap the values

 

 

Upvote
Downvote
Loading...

Input

10 20

Expected Output

a = 20 b = 10