Code.GeekInterview.com
 
Code Samples C++
 

Swaping Two Numbers


Code ResourceAuthor: prabha  

Difficulty Level:

Published: 12th Sep 2007   Read: 6193 times  

Filed in: C++
Add Comment


 

 

Sponsored Links


 

 

Swaping of two Numbers without 3rd variable in C++.

 


Sample Code
  1.  
  2. void main()
  3. {
  4.    int a,b;
  5.    printf("Enter any two no.s");
  6.    scanf("%d%d",&a,&b);
  7.    
  8.    printf("nBefore swaping a=%d,b=%d",a,b);
  9.    a=a+b;
  10.    b=a-b;
  11.    a=a-b;
  12.    printf("n After swaping a=%d,b=%d",a,b);
  13.    getch();
  14. }
  15.  
Copyright GeekInterview.com


Next Article: C++ Elliptic Curve library


 

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

Great and logically correct
Comment posted by: babu_krishi on 2007-10-09T05:37:58
1) int main() invokes undefined behavior.
2) The method poses overflow problems, so swapping two numbers which are greater than half of the limited int size gives incorrect result.
A better method for swapping two integers a and b is
a^=b;
b^=a;
a^=b;
Comment posted by: jagatsastry on 2007-10-22T02:03:48

Popular Coders

# Coder NameHits
1. Priya36671
2. chowsys14958
3. bipin_vaylu13579
4. raghu.mohindru8719
5. Subashini7235
6. vimal.fire6981
7. rachittyagi6630
8. venki_madesh6209
9. prabha6194
10. vipin gupta5907

Active Coders

Refined Tags