Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // program to swap 2 numbers using different methods
- #include<stdio.h>
- #include<conio.h>
- // method that swaps numbers using + and - operations
- void swap1(int *num1, int *num2){
- *num1 = *num1 + *num2;
- *num2 = *num1 - *num2;
- *num1 = *num1 - *num2;
- return;
- }
- // method that swaps numbers using * and / operations
- void swap2(int *num1, int *num2){
- *num1 = *num1 * *num2;
- *num2 = *num1 / *num2;
- *num1 = *num1 / *num2;
- return;
- }
- // method that swaps numbers using ^ ( ex-or ) operations
- void swap3(int *num1, int *num2){
- *num1 = *num1 ^ *num2;
- *num2 = *num1 ^ *num2;
- *num1 = *num1 ^ *num2;
- return;
- }
- // method that swaps numbers using temp variables
- void swap4(int *num1, int *num2){
- int temp;
- temp = *num1;
- *num1 = *num2;
- *num2 = temp;
- return;
- }
- void main(){
- int a = 10, b = 15;
- float c = 10.999,d = 11.222;
- printf("Values before swapping are a = %d and b = %d", a,b);
- swap1(&a, &b);
- printf("\nValues after swapping using swap1 are :");
- printf("\na = %d \nb = %d",a,b);
- swap2(&a, &b);
- printf("\nValues after swapping using swap2 are :");
- printf("\na = %d \nd = %d",a,b);
- swap3(&a, &b);
- printf("\nValues after swapping using swap3 are :");
- printf("\na = %d \nb = %d",a,b);
- swap4(&a, &b);
- printf("\nValues after swapping using swap4 are :");
- printf("\na = %d \nb = %d",a,b);
- return;
- }
Add Comment
Please, Sign In to add comment