Write a C Program to Swap two numbers using Pointers

Write a C Program to Swap two numbers using Pointers


 #include <stdio.h>
 // function to swap the two numbers
 void swap(int *x,int *y)
 {
    int t;
     t   = *x;
    *x   = *y;
    *y   =  t;
 }



 int main()
 {
    int num1,num2;

    printf("Enter value of num1: ");
    scanf("%d",&num1);
    printf("Enter value of num2: ");
    scanf("%d",&num2);

    //displaying numbers before swapping
    printf("Before Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

    //calling the user defined function swap()
    swap(&num1,&num2);



    //displaying numbers after swapping
    printf("After  Swapping: num1 is: %d, num2 is: %d\n",num1,num2);

    return 0;
 }
          

Output:

 Enter value of num1: 5
 Enter value of num2: 8
 Before Swapping: num1 is: 5, num2 is: 8
 After  Swapping: num1 is: 8, num2 is: 5



 --------------------------------