C++ program to swap two numbers using pointers

C++ program to swap two numbers using pointers

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int *a,*b,*temp;
cout<<“Enter value of a and b:”;
cin>>*a>>*b;

temp=a;
a=b;
b=temp;

cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b;
getch();
}

2 thoughts on “C++ program to swap two numbers using pointers

  1. David Thiessen

    Here is how I rewrote:

    //this program will swap two ints using ptrs

    #include

    using namespace std;

    int 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 << "nAfter swapingna=" << *a << "nb=" << *b;

    delete a;
    delete b;

    return 0;
    }

    Reply
  2. Fathin Maisarah

    int* a = new int;
    int* b = new int;

    hi can you explain to me what is the use of this part of coding ?

    Reply

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.