c++ programs examples

C++ Program – Swap two numbers without using a 3rd variable using function with arguments.

c++ programs examples
/* 15. Write a program to swap two numbers without using third variable
  using function with argument*/

 #include<iostream.h>
 #include<conio.h>
 void swap(int*,int*);
 void main()
 {
  clrscr();
  int a,b;
  cout<<"Enter value for a="<<endl;
  cin>>a;
  cout<<"Enter value for b="<<endl;
  cin>>b;
  swap(&a,&b);
  cout<<"After swapping:"<<endl<<"a="<<a<<" "<<"b="<<b<<endl;
  getch();
  }
  void swap(int *a,int *b)
  {
    *a=*a+*b;
    *b=*a-*b;
    *a=*a-*b;
   }

Leave a Reply

Your email address will not be published. Required fields are marked *