Sunday, July 12, 2015

Swapping of Two Numbers in C++ using Pointers

In this article I would like to share with you a  program that I wrote in C++ that will interchange or swap the arrangement of numbers using pointers as our data structure.

Feel free to use my code in your programming projects at school or work.  If you have some questions please send me an email at jakerpomperada@gmail.com and jakerpomperada@yahoo.com.

People here in the Philippines can reach me at my mobile number 09173084360.

Thank you very much and Happy Programming.


Program Listing

#include <iostream>

using namespace std;

int swap(int *a, int *b)
{
    int temp=0;

    temp = *a;
    *a = *b;
    *b = temp;
}

main() {
    int a=0,b=0;
    cout << "\t\t Swap Two Numbers ";
    cout << "\n\n";

    cout << "Enter two numbers  ==> "; swap(&a,&b);
    cout << "\n\n";
    cout <<"Swap Arragement ==> "
         << a << " " << b;
    cin >> a >> b;
    cout << "\n\n";
    cout <<"Original Arragement ==> "
           << a << " " << b;

    cout << "\n\n";
    system("pause");
}

No comments:

Post a Comment