swapping of two numbers in c without temporary variable
#include<stdio.h>
void main()
{
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
}
swapping two numbers in c
//Author: Subodh
//! Swap two number using XOR operation
cout << "Before, n1 = " << num1 << ", n2 = " << num2 << endl;
num1 = num1 ^ num2, num2 = num1 ^ num2, num1 = num1 ^ num2;
cout << "After, n1 = " << num1 << ", n2 = " << num2 << endl;
swapping of two numbers without using third variable in c
#include<stdio.h>
void main()
{
int x = 10, y = 20;
printf("Before swap x=%d y=%d",x,y);
x=x+y;
y=x-y;
x=x-y;
printf("\nAfter swap x=%d y=%d",x,y);
}
// swap variables in C
void swap(int *x, int *y){
int temp = *x;
*x = *y;
*y = temp;
}
swap(&a, &b); // address of a and b
c program for swapping of two numbers using temporary variable
#include <stdio.h>
int main()
{
int a, b, temp;
printf("enter the values of a and b: \n");
scanf("%d%d", &a, &b );
printf("current values are:\n a=%d\n b=%d\n", a, b);
temp=a;
a=b;
b=temp;
printf("After swapping:\n a=%d\n b=%d\n", a, b);
}