C++ Value Swapping (CVS)



AVAILABLE:    5

When we revisit the fundamental theory of algorithms and programming, we often begin by understanding the process or steps required to solve a problem. Lecturers everywhere typically provide a simple illustration of the basic concept of an algorithm. One common example is: "What are the steps to cook instant noodles or prepare a meal?"

When we move to the foundational concepts of programming, we are often introduced to a simple algorithm known as "Value Swapping." This is usually illustrated with three glasses, where one or two of them contain a liquid, and the task is to swap the contents between the glasses.

With that in mind, let's explore how a C++ program for swapping works. Check out the example below:

Value Swapping in C++

Source Code:

#include <iostream.h>
#include <conio.h>

void main() {
    int a, b, c;
    cout << "Value Swapping\n";
    cout << "Enter value 1: "; cin >> a;
    cout << "Enter value 2: "; cin >> b;
    c = a;
    a = b;
    b = c;
    cout << "After swapping:\n";
    cout << "Value 1: " << a << endl;
    cout << "Value 2: " << b << endl;

    getch();
}

Hopefully, this is helpful, and happy learning! 😊


Post a Comment

Previous Next

نموذج الاتصال