How to Swap Two Integers in C++

In this Problem we Swap the two integers in C++ Program. We can also change the Value of a Variable using Pointers in C++ but here we use three Variables to Swap the Value of two Integers . The First two Variables Hold their real values while the Third Variable is use to Swap their Variables.

Source Code

#include <iostream>         
using namespace std;

/* Programme to Swap the Value of two Integers */

int main()
 {
  int number1 = 0;   // Intilize a variable for number 1
  int number2 = 0; // Intilize a variable for number 2
  int swap = 0;  // Intilize a variable for swaping the value
  cout<<endl<<"************ VALUE SWAP ***************"<<endl;
  cout<<endl<<"Enter Number_1 : ";
  cin>>number1;  // Reccieveing from the Keybord
  cout<<endl<<"Enter Number_2 : ";
  cin>>number2;  //Reccieveing from the Keybord
  swap = number1;  // in Variable Swap save the value of number 1
  number1 = number2; // in Number 1 value save the value of number 2
  number2 = swap;  // in number 2 save the value of Swap and in swap there is value of Number 1
  cout<<endl<<endl<<"After Swap Number_1 : "<<number1<<endl; // Display the number 1 after Swapping
  cout<<endl<<endl<<"After Swap Number_2 : "<<number2<<endl; //// Display the number 1 after Swapping
 return 0;
}

Output of the Program

Values are Swapped


Share on Google Plus

About Asad

Asad Niazi is Software Engineer , Programmer, Web Developer and a young mentor of BloggersTown and PProgramming. Asad Love to writes about Technology, Programming, Blogging and make money online.

4 comments:

  1. How can i send my problem to you ???????????????????????????

    ReplyDelete
  2. please upload android code and some interesting apps list ...waiting

    ReplyDelete