Learn C++ beginer programs

July 7, 2009

write a program to swap two numbers without using third variable

Filed under: cin,cout,swapping — snehilkhanor @ 3:40 pm
Two numbers are taken from user and then swapped without using a third variable.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a,b;
  7. cout<<”Enter a: “;
  8. cin>>a;
  9. cout<<”Enter b: “;
  10. cin>>b;
  11. cout<<”Before swapping:”<<endl;
  12. cout<<”a: “<<a<<” b: “<<b<<endl;
  13. a=a+b;
  14. b=a-b;
  15. a=a-b;
  16. cout<<”After swapping:”<<endl;
  17. cout<<”a: “<<a<<” b: “<<b;
  18. getch();
  19. }

write a program to swap two numbers using a third variable

Filed under: cin,cout,swapping — snehilkhanor @ 3:28 pm
Two numbers are taken from the user and then swapped using a third variable.
Select To use this code as it is.. select and copy paste this code into code.cpp file :)
  1. #include<iostream.h>
  2. #include<conio.h>
  3. void main()
  4. {
  5. clrscr();
  6. int a,b,c;
  7. cout<<”Enter a: “;
  8. cin>>a;
  9. cout<<”Enter b: “;
  10. cin>>b;
  11. cout<<”Before swapping:”<<endl;
  12. cout<<”a: “<<a<<” b: “<<b<<endl;
  13. c=a;
  14. a=b;
  15. b=c;
  16. cout<<”After swapping:”<<endl;
  17. cout<<”a: “<<a<<” b: “<<b;
  18. getch();
  19. }

Theme: Rubric. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.