/*
Md. Alamgir Hossain
Lecturer, Dept. of CSE, Prime University
*/
//C program to swap two numbers
//Input: a = 10, b = 25
//Output: b = 25, a = 10
#include<stdio.h>
int main()
{
int a, b, temp;
a = 10;
b = 25;
printf("Before swapping: a = %d, b = %d\n",a, b);
/*
temp = a;
a = b;
b = temp;
*/
/*
Using arithmetic operators + and -
a = a + b;
b = a - b;
a = a - b;
*/
/*
Using arithmetic operators * and /
a = a * b;
b = a / b;
a = a / b;
*/
/*
Using bitwise XOR operator ^
a = a ^ b;
b = a ^ b;
a = a ^ b;
*/
//Using bitwise XOR operator ^ in one line
// a ^= b ^= a ^= b;
//Using arithmetic operators + and - in one line
//a = (a + b) - (b = a);
//Using arithmetic operators * and / in one line
//a = (a * b) / (b = a);
printf("After swapping: a = %d, b = %d\n",a, b);
return 0;
}
No comments:
Post a Comment