Code.GeekInterview.com
 
Code Samples C
 

Swap 2 variables without using 3rd variable


Code ResourceAuthor: deepu08  

Difficulty Level: Beginner

Published: 3rd Jun 2009   Read: 21247 times  

Filed in: C
Add Comment


 

 

Sponsored Links


 

 

This program swaps 2 varibles with out using of 3rd variable.

 


Sample Code
  1. #include<stdio.h>
  2. void main()
  3. {
  4.  int a,b;
  5.  printf("enter 2 numbers");
  6.  scanf("%d %d",&a,&b);
  7.  printf("nthe 2 numbers are %d %d",a,b);
  8.  a=a^b;
  9.  b=a^b;
  10.  a=a^b;
  11.  printf("n the 2 numbers are %d %d",a,b);
  12. }
Copyright GeekInterview.com


Next Article: Modified String Encryption


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Comments

it's good
Comment posted by: manoranjith on 2010-08-20T07:06:48
Hi Imran T Patel here If any thing Wrong with this Program please forward to me...
We can also Write Swap Program with using pointer but without using 3rd Variable

#include
void main()
{
int a=10;
int b=20;
*(&a+1)=*(&a);
a=b;
b=*(&a+1);
printf("n THe value of-------- a---%d b---%d",a,b);
getch();
}


Comment posted by: Imran T Patel on 2010-08-30T07:11:10
Good !
Comment posted by: Mainak Dutta on 2011-03-14T23:09:14
how to code for 20x4 lcd using embedded c in mp lab
Comment posted by: Raghu on 2011-03-08T05:41:11

Popular Coders

# Coder NameHits
1. kaivalya198946522
2. mano.mithun37072
3. deepu0821248
4. sadasivathavamani13705
5. Jimmy Zorald12176
6. venkatakrishnansvpr12018
7. meefriend4ever10232
8. shashivaishnav9928
9. chandrikakr9916
10. sripri8279

Active Coders

Refined Tags