Code
/*
* -----------------------------------------------------------
* Logic Building with Computer Programming (CSU1128P)
* Instructor: Abdullahi Adem | Author: Divya Mohan
*
* This code is a part of the educational initiative by dmj.one
* with aim of empowering and inspiring learners in the field of
* Computer Science and Engineering through the respective courses.
*
* (c) 2022, Divya Mohan for dmj.one. All rights reserved.
* -----------------------------------------------------------
*/
#include <stdio.h>
swap (int *x, int *y){
int z;
z = *x;
*x = *y;
*y = z;
}
int main()
{
int d, m, j;
printf("Enter 2 numbers to swap with space and without decimal - (Example: 20 50) - and press enter: ");
scanf("%d %d", &d, &m);
printf("\n \t Before Swapping: %d %d \n", d, m);
// Method 1
j = d;
d = m;
m = j;
printf("\n \t After Swapping: %d %d \n", d, m);
// Method 2
d = d + m;
m = d - m;
d = d - m;
printf("\n \t After Swapping again: %d %d \n", d, m);
// Method 3
d = d * m;
m = d / m;
d = d / m;
printf("\n \t After Swapping again: %d %d \n", d, m);
// Method 4
d = d ^ m;
m = d ^ m;
d = d ^ m;
printf("\n \t After Swapping again: %d %d \n", d, m);
// Method 5
(d ^= m), (m ^= d), (d ^= m);
printf("\n \t After Swapping again: %d %d \n", d, m);
// Method 6
swap(&d, &m);
printf("\n \t After Swapping: %d %d \n", d, m);
return 0;
}
Output
Enter 2 numbers to swap with space and without decimal - (Example: 20 50) - and press enter: 3 5
Before Swapping: 3 5
After Swapping: 5 3
After Swapping again: 3 5
After Swapping again: 5 3
After Swapping again: 3 5
After Swapping again: 5 3
After Swapping: 3 5