## INDEX

### C program for swapping of two numbers

Code for swapping in c

#include<stdio.h>
int main(){
int a,b,temp;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);
printf("Before swapping: a = %d, b=%d",a,b);

temp = a;
a = b;
b = temp;
printf("\nAfter swapping: a = %d, b=%d",a,b);

return 0;
}

C program for swapping of two numbers using pointers

#include<stdio.h>

int main(){

int a,b;
int *ptra,*ptrb;
int *temp;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

printf("Before swapping: a = %d, b=%d",a,b);

ptra = &a;
ptrb = &b;

temp = ptra;
*ptra = *ptrb;
*ptrb = *temp;

printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}

Sample output:
Enter any two integers: 5 10
Before swapping: a = 5, b=10
After swapping: a = 10, b=10

Swapping program in c using function

#include<stdio.h>

void swap(int *,int *);
int main(){

int a,b;

printf("Enter any two integers: ");
scanf("%d%d",&a,&b);

printf("Before swapping: a = %d, b=%d",a,b);

swap(&a,&b);

printf("\nAfter swapping: a = %d, b=%d",a,b);
return 0;
}

void swap(int *a,int *b){
int *temp;
temp = a;
*a=*b;
*b=*temp;
}

Sample output:
Enter any two integers: 3 6
Before swapping: a = 3, b=6
After swapping: a = 6, b=6

1. the first program should have

b=temp;

2. jyothi.vit2/2/12, 4:49 PM

logic: swapping

a=a+b;
b=a-b;
a=a-b;

3. in the program for swapping 2 nos. it should be *temp= *ptra

4. swapping without 3rd variable
a=a+b-a;
b=a+b-b;

1. its wnt work... :|
a=a^b;
b=a^b;
a=a^b;

2. yaa itz right

3. This is single line logic:

b = b + a - (a = b);

5. swapping using ptr is wrong..
should be
int temp;
temp=*a;

6. there r lots of mistakes

7. This comment has been removed by the author.

8. swapping in oneline is needed?

9. Very Nice Article helps a lot ...
You can also find 6 type of logic to swap two number here check one it will be helpful ...
Swapping of two numbers

10. Very Nice Article helps a lot ...
You can also find 6 type of logic to swap two number here check one it will be helpful ...
Swapping of two numbers

11. This comment has been removed by the author.

12. also use this
==========
a=a-(~b)-1
b=a+(~b)+1
a=a+(~b)+1
=========
here ~b=(-b-1)

1. Superb nice

2. Superb nice

3. Good logic for swapping of 2 nos

13. need to keep * in temp assigning in swap function

14. logic is wrong for swapping using function...

correct one iis

void swap(int*a,int*b)
{
int temp;
temp=*b;
*b=*a;
*a=temp;
}