Sunday, February 5, 2017

Swapping of Two Numbers in C++

Here is a sample program that I wrote a long time ago while I'm working as a college professor teaching C++ programming subjects. This type of program will ask the user to give two numbers and then it will swap or interchange the arrangement of the two numbers given by our user. I am using CodeBlocks as my C++ editor and Dev C++ as my C++ compiler all of them are free to download over the Internet. Thank you.

Add me at Facebook my address is jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My email address are the following jakerpomperada@gmail.com and jakerpomperada@yahoo.com

My mobile number here in the Philippines is 09173084360.


Program Listing

#include <iostream>

using namespace std;

 int temp=0;


class swapping {

    public :
 int values[1];

    int swap_number(int a, int b)
    {
      temp = values[0];
     values[0] =values[1];
     values[1] = temp;
    }
};

 main() {
     swapping numbers;

      cout << "\t\t Swapping of Values 1.0";
    cout << "\n\n";
    cout << "Enter first number : ";
    cin >> numbers.values[0];
    cout << "Enter second number : ";
     cin >> numbers.values[1];
     cout << "Before the Swapping of values ";
     cout << "\n\n";
    cout << numbers.values[0] << " " << numbers.values[1];

     numbers.swap_number(numbers.values[0],numbers.values[1]);

     cout << "\n\n";
     cout << "After the Swapping of values ";
     cout << "\n\n";
     cout << numbers.values[0] << " " << numbers.values[1];
     cout << "\n\n\n";
     system("PAUSE");
}




No comments:

Post a Comment