swapping two numbers program
Swapping two Numbers using a Temporary Variable
Below is a program to swap two numbers using temporary variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 10, y = 15, temp;
temp = x;
x = y;
y = temp;
printf("x = %d and y = %d", x, y);
getch();
}
x = 15 and y = 10
Swapping tow Numbers without using a Temporary Variable
Below is a program to swap two numbers without using any temporary variable.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 10, y = 15;
x = x + y - (y = x);
printf("x = %d and y = %d",x,y);
getch();
}
x = 15 and y = 10
Swapping two Numbers using Bitwise Operator
Below is a program to swap two numbers using bitwise operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 6, y = 4;
x = x^y;
y = x^y;
x = x^y;
printf("x = %d and y = %d", x, y);
getch();
}
x = 4 and y = 6
Swapping two Numbers using Multiplication and Division
Below is a program to swap two numbers using multiplication and division.
#include<stdio.h>
#include<conio.h>
void main()
{
int x = 6, y = 4;
x = x*y;
y = x/y;
x = x/y;
printf("x = %d and y = %d", x, y);
getch();
}
x = 4 and y = 6