Tuesday 15 July 2014

How to Swap two Integers in C++

Structure of the Problem Requirements 


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 two 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
Both Number's Swapping there Value





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.

3 comments: Post Yours! Read Comment Policy!▼
Important Note:
We have Zero Tolerance to Spam. Chessy Comments and Comments with Links will be deleted immediately upon our review.

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

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

    ReplyDelete