C Program To Swap Two Numbers using Pointers
Learn How To Swap Two Numbers using Pointers in C Programming Language. Two variables can be swapped using Functions and without using Functions as well. Learn C Code To Swap Two Variable Addresses of their locations of the Variables. This program to Swap Two Variables using Pointers takes the Call By Reference approach in C Programming Language.
Also Read: C Program To Swap Two Variables using Temporary (Third) Variable
C Program To Swap Two Numbers using Pointers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include<stdio.h> int swap(int *ptr1, int *ptr2); int main() { int num1, num2; printf("\nEnter The First Number:\t"); scanf("%d", &num1); printf("\nEnter The Second Number:\t"); scanf("%d", &num2); swap(&num1, &num2); printf("\nThe Swapped Values are:\n"); printf("First Number = %d and Second Number = %d\n", num1, num2); return 0; } int swap(int *ptr1, int *ptr2) { int temp; temp = *ptr1; *ptr1 = *ptr2; *ptr2 = temp; } |
Also Read: C Program To Swap Two Variables using Call By Reference
Output

Also Read: C Program To Swap Two Variables using Call By Value
If you have any compilation errors or doubts in this C Program To Swap Variables with Pointers method, let us know about it in the Comment Section below.