Showing posts with label How to swap two integers using pointers. Show all posts
Showing posts with label How to swap two integers using pointers. Show all posts

Friday, 30 January 2015

Swap Two Integers using Pointers

Structure of the Problem Requirements

This Program swap two numbers using pointers in C++ . Pointers contain the memory address of the variable and also used to allocate the dynamic memory in the program. In this problem there are two pointers and swap their values with simple logic.

Source Code

#include<iostream>
using namespace std;
int main ()
{
int a, b, *p1,*p2;
cout<<"\t \t \t LEP Tutorials \n \n \n ";
cout<<" Enter Your First Number : ";
cin>>a;
cout<<" Enter Your Second Number: ";
cin>>b;
p1 = &a;
p2 = &b;
a = *p1 + *p2;
b = *p1 - *p2;
a = *p1 - *p2;
cout<<" Number After Swapping : "<< a <<endl;
cout<<" Number After Swapping : "<< b <<endl;
}

Output of the Program

Pointer to Swap Two Integers