Problem: Write a program that swaps two numbers using pointers.
Source Code:
- #include <stdio.h>
 - int main()
 - {
 - //variable declearation.
 - int x, y, *a, *b, temp;
 - //Input any number from the user
 - printf("Enter the value of x and y\n");
 - scanf("%d%d", &x, &y);
 - printf("Before Swapping\nx = %d\ny = %d\n", x, y);
 - //Compute swapping of the number
 - a = &x;
 - b = &y;
 - temp = *b;
 - *b = *a;
 - *a = temp;
 - //display the swap number.
 - printf("After Swapping\nx = %d\ny = %d\n", x, y);
 - return 0;
 - }
 
Output:
Output 1:
Enter the value of
x and y
56
45
Before Swapping
x = 56
y = 45
After Swapping
x = 45
y = 56
Output 2:
Enter the value of x and y
26
89
Before Swapping
x = 26
y = 89
After Swapping
x = 89
y = 26
0 comments:
Post a Comment