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

Share it Please
asad

About Author!

Asad Niazi is Software Engineer , Programmer, Web Developers and a young mentor of Tech Solutions Desk and Blogging Solutions . Asad Love to writes about Technology, Programming, Blogging and make money online.

0 comments:

Post a Comment