Friday, March 16, 2018

Swap of Two Numbers using Reference in C++

A very simple program that I wrote to show you how to swap two numbers using the concept of reference in C++.


I am currently accepting programming and web development work kindly contact me in the following email address for further details. Thank you.
My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.

My telephone number at home here in Bacolod City, Negros Occidental Philippines is  +63 (034) 4335675.





Sample Program Output


Program Listing

swap.cpp


#include <iostream>

using namespace std;


        void swap(int &x, int &y);

       int main()
       {
          
         int x=0,y=0;
         
  cout << "Swap of Two Numbers using Reference in C++";
   cout << "\n\n";
   cout << "Created By Mr. Jake R. Pomperada, MAED-IT";
   cout << "\n\n";
   cout << "Enter values for x and y : ";
   cin >> x >> y;

       cout << "Main. Before swap, x: " << x << " y: " << y << "\n";
         swap(x,y);
         cout << "Main. After swap, x: " << x << " y: " << y << "\n";
        cout << "\n\n";
   cout << " End of Program ";  
           }


          void swap (int &ax, int &ay)
        {
             int temp;

                cout << "Swap. Before swap, ax: " << ax << " ay: " << ay << "\n";
                temp = ax;
                ax = ay;
              ay = temp;

                cout << "Swap. After swap, ax: " << ax << " ay: " << ay << "\n";
                
            }


No comments:

Post a Comment