Swap 2 Numbers using Pointers in C
Here, you will get and learn the program code to swap 2 numbers using pointers in c programming language.
Swapping is a method to interchange the values of two variable.
Program to Swap 2 Numbers using Pointers in C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include <stdio.h> #include<conio.h> int main() { int *x, *y, temp; clrscr(); printf("\nEnter the value of x = "); scanf("%d", x); printf("\nEnter the value of y = "); scanf("%d", y); printf("\nNumbers Before Swapping\nx = %d\ny = %d\n", *x, *y); temp = *x; *x = *y; *y = temp; printf("\nNumbers After Swapping\nx = %d\ny = %d\n", *x, *y); getch(); return 0; } |
Output
Enter the value of x =4
Enter the value of y =7
Numbers Before Swapping
x = 4
y = 7
Numbers After Swapping
x = 7
y = 4
Check out our other C programming Examples