Procedure-1 : ( Using 3rd Variable)
void swap(int a, int b)
{
int temp;
temp = a;
m = b;
b = temp;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
{
int temp;
temp = a;
m = b;
b = temp;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
Procedure-2 : ( Without Using 3rd Variable)
void swap(int a, int b)
{
a = a + b;
b = a - b;
a = a - b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
{
a = a + b;
b = a - b;
a = a - b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
Procedure-3 : (Without Using third variable: with using Bitwise
operator)
void swap(int a, int b)
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
OR
void swap(int m, int n)
{
m ^= n ^= m ^= n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
{
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
OR
void swap(int m, int n)
{
m ^= n ^= m ^= n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
Procedure-4 : (Without Using 3rd Variable
but both numbers must not be equal to Zero)
void swap(int a, int b)
{
if(a != 0 && b != 0)
{
a = a * b;
b = a/b;
a = a/b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
{
if(a != 0 && b != 0)
{
a = a * b;
b = a/b;
a = a/b;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
Procedure-5 : Swapping in a
Single Line
{
b = a + b - (a = b);
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",a);
printf("\nSecond Number is: %d",b);
Enjoy Programming!!!
No comments:
Post a Comment