1#include <stdio.h>
2int main() {
3 double a, b;
4 printf("Enter a: ");
5 scanf("%lf", &a);
6 printf("Enter b: ");
7 scanf("%lf", &b);
8
9 // Swapping
10
11 // a = (initial_a - initial_b)
12 a = a - b;
13
14 // b = (initial_a - initial_b) + initial_b = initial_a
15 b = a + b;
16
17 // a = initial_a - (initial_a - initial_b) = initial_b
18 a = b - a;
19
20 printf("After swapping, a = %.2lf\n", a);
21 printf("After swapping, b = %.2lf", b);
22 return 0;
23}
24
1#include <stdio.h>
2
3void swap(int*, int*);
4
5int main()
6{
7 int x, y;
8
9 printf("Enter the value of x and y\n");
10 scanf("%d%d",&x,&y);
11
12 printf("Before Swapping\nx = %d\ny = %d\n", x, y);
13
14 swap(&x, &y);
15
16 printf("After Swapping\nx = %d\ny = %d\n", x, y);
17
18 return 0;
19}
20
21void swap(int *a, int *b)
22{
23 int temp;
24
25 temp = *b;
26 *b = *a;
27 *a = temp;
28}
29
1#include <stdio.h>
2int main()
3{
4 int a, b, temp;
5 printf("enter the values of a and b: \n");
6 scanf("%d%d", &a, &b );
7 printf("current values are:\n a=%d\n b=%d\n", a, b);
8 temp=a;
9 a=b;
10 b=temp;
11 printf("After swapping:\n a=%d\n b=%d\n", a, b);
12}
1#include <stdio.h>
2#include <stdlib.h>
3
4int main()
5{
6 //initialize variables
7 int num1 = 10;
8 int num2 = 9;
9 int tmp;
10
11 //create the variables needed to store the address of the variables
12 //that we want to swap values
13 int *p_num1 = &num1;
14 int *p_num2 = &num2;
15
16 //print what the values are before the swap
17 printf("num1: %i\n", num1);
18 printf("num2: %i\n", num2);
19
20 //store one of the variables in tmp so we can access it later
21 //gives the value we stored in another variable the new value
22 //give the other variable the value of tmp
23 tmp = num1;
24 *p_num1 = num2;
25 *p_num2 = tmp;
26
27 //print the values after swap has occured
28 printf("num1: %i\n", num1);
29 printf("num2: %i\n", num2);
30
31 return 0;
32}
33
1#include <stdio.h>
2
3int main()
4{
5 int x = 20, y = 30, temp;
6 temp = x;
7 x = y;
8 y = temp;
9 printf("X = %d and Y = %d", x, y);
10
11 return 0;
12}
1#include<stdio.h>
2
3void swapping(int, int); //function declaration
4int main()
5{
6 int a, b;
7
8 printf("Enter values for a and b respectively: \n");
9 scanf("%d %d",&a,&b);
10
11 printf("The values of a and b BEFORE swapping are a = %d & b = %d \n", a, b);
12
13 swapping(a, b); //function call
14 return 0;
15}
16
17void swapping(int x, int y) //function definition
18{
19 int third;
20 third = x;
21 x = y;
22 y = third;
23
24 printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
25}