In this post, we are going to make a program in C to Swap two numbers using Pointers.
Algorithm
1. Declare K and L with Pointers
2. Printing Before Swap Numbers
3. Using Formula to Swap with Pointers
4. Printing After Swap Numbers
Code
#include <stdio.h>
int main()
{
int k=10,l=20,*p1=&k,*p2=&l;
printf("Before swap : *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\n After swap : *p1=%d *p2=%d",*p1,*p2);
return 0;
}
Output
Before swap : *p1=10 *p2=20
After swap : *p1=20 *p2=10