1// SWAPPING WITHOUT USING THIRD VARIABLE
2#include<stdio.h>
3 int main()
4{
5int a=10, b=20;
6printf("Before swap a=%d b=%d",a,b);
7a=a+b;//a=30 (10+20)
8b=a-b;//b=10 (30-20)
9a=a-b;//a=20 (30-10)
10printf("\nAfter swap a=%d b=%d",a,b);
11return 0;
12}
1// SWAP USING THIRD VARIBLE
2#include <stdio.h>
3int main()
4{
5int var1, var2, temp;
6printf("Enter two integersn");
7scanf("%d%d", &var1, &var2);
8printf("Before SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
9temp = var1;
10var1 = var2;
11var2 = temp;
12printf("After SwappingnFirst variable = %dnSecond variable = %dn", var1, var2);
13return 0;
14}
1//The code for a function to Swap two number with a temporary variable is as follows
2#include<stdio.h>
3
4void swapping(int, int); //function declaration
5int main()
6{
7 int a, b;
8
9 printf("Enter values for a and b respectively: \n");
10 scanf("%d %d",&a,&b);
11
12 printf("The values of a and b BEFORE swapping are a = %d & b = %d \n", a, b);
13
14 swapping(a, b); //function call
15 return 0;
16}
17
18void swapping(int x, int y) //function definition
19{
20 int third;
21 third = x;
22 x = y;
23 y = third;
24
25 printf("The values of a and b AFTER swapping are a = %d & b = %d \n", x, y);
26}