Showing posts with label C Language. Show all posts
Showing posts with label C Language. Show all posts
Friday, April 8, 2011
Swapping with and without third Variable ( C Program).
//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();
}
Subscribe to:
Posts (Atom)