Tuesday, December 26, 2017

C++ Code Review: Swaps two numbers using pointer.

Description: This program swaps two numbers using pointer.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program swaps two numbers using pointer

 

#include<iostream>

#include<conio.h> //for getch()

 

using namespace std;

 

main()

{

    int* a = new int;

    int* b = new int;

 

    cout << "Enter value of a and b:";

    cin >> *a >> *b;

 

//put a in temp

    int* temp = a;

//put b in a

    a = b;

//put temp, was a, into b

    b = temp;

 

    cout << "After swapping \n a=" << *a << "\n b=" << *b;

 

    delete a;

    delete b;

    return 0;

}               

 

Output:

 clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment