Using 3rd Variable
Without Using 3rd Variable
Without Using 3rd Variable
OR
void swap(int m, int n)
Swapping in a Single Line
void swap(int m, int n)
{
int temp;
temp = m;
m = n;
n = temp;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
{
int temp;
temp = m;
m = n;
n = temp;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
Without Using 3rd Variable
void swap(int m, int n)
{
m = m + n;
n = m - n;
m = m - n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
{
m = m + n;
n = m - n;
m = m - n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
Without Using 3rd Variable
void swap(int m, int n)
{
m = m ^ n;
n = m ^ n;
m = m ^ n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
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);
}
{
m = m ^ n;
n = m ^ n;
m = m ^ n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
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);
}
Without Using 3rd Variable but both numbers should be Non-Zero
void swap(int m, int n)
{
if(m != 0 && n != 0)
{
m = m * n;
n = m/n;
m = m/n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
void swap(int m, int n)
{
if(m != 0 && n != 0)
{
m = m * n;
n = m/n;
m = m/n;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
OR
void swap(int m, int n)
{
if(m != 0 && n != 0)
{
n = m * n;
m = n/m;
n = n/m;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
if(m != 0 && n != 0)
{
n = m * n;
m = n/m;
n = n/m;
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
else
{
printf("\n Both the numbers should be Non-Zero!");
}
}
Swapping in a Single Line
void swap(int m, int n)
{
n = m + n - (m = n);
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}
{
n = m + n - (m = n);
printf("\nAfter Swapping, the numbers are: ");
printf("\n\nFirst Number is: %d",m);
printf("\nSecond Number is: %d",n);
}