//simple way of swapping
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
int b=10;
int c;
clrscr();
printf("Before swapping:\na=%d \nb=%d",a,b);
//swaping
c=a;
a=b;
b=c;
printf("\nAfter swapping:\na=%d \nb=%d",a,b);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5;
int b=10;
int c;
clrscr();
printf("Before swapping:\na=%d \nb=%d",a,b);
//swaping
c=a;
a=b;
b=c;
printf("\nAfter swapping:\na=%d \nb=%d",a,b);
getch();
}
//swapping without third variable
//A tricky way of swapping
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf("Before swapping:\na=%d\nb=%d",a,b);
a=b+a;
b=a-b;
a=a-b;
printf("\nAfter swapping:\na=%d\nb=%d",a,b);
getch();
}
//A tricky way of swapping
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=20;
clrscr();
printf("Before swapping:\na=%d\nb=%d",a,b);
a=b+a;
b=a-b;
a=a-b;
printf("\nAfter swapping:\na=%d\nb=%d",a,b);
getch();
}
second one is good A tricky method of swapping
ReplyDeletevery helpful thank you
Website design Essex