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();
}

   

 //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();
}

1 comment:

  1. second one is good A tricky method of swapping
    very helpful thank you

    Website design Essex

    ReplyDelete