Code

#include <stdio.h>

void swap(int *p1, int *p2) {
    int temp = *p1;
    *p1 = *p2;
    *p2 = temp;
}

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

  • Take two integers as input (a and b).
  • Call the swap() function with their addresses.
  • Inside swap(), use a temporary variable to exchange their values using pointers.
  • Print the swapped values of a and b

 

 

Upvote
Downvote
Loading...

Input

10 20

Expected Output

a = 20 b = 10