Code.GeekInterview.com
  I am new, Sign me up!
 
Code Samples C++
 

Swaping Two Numbers


Code ResourceAuthor: prabha  

Difficulty Level:

Published: 12th Sep 2007   Read: 2406 times  

Filed in: C++
Add Comment


 


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 Comment


Members Please Login

Name:  Email: (Optional. Used for Notification)

Title:
 
Comment:
Validation Code: <=>  (Enter this code in text box)





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. Priya10967
2. bipin_vaylu4908
3. chowsys4865
4. raghu.mohindru4323
5. venki_madesh3221
6. vimal.fire2914
7. Subashini2442
8. prabha2407
9. vipin gupta2016
10. Mehedi Shams Rony1802

Active Coders

Refined Tags

 

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape