Photo by Antonio Batinić
Here’s a simple C++ program to swap two numbers, along with a detailed explanation:
#include <iostream>
// This is a preprocessor directive that includes the standard input-output stream library
using namespace std;
// This line tells the compiler to use the standard namespace
int main() {
// This is the main function, the entry point of a C++ program
int num1, num2, temp;
// Declaration of integer variables
// Prompt the user to enter two numbers
cout << "Enter first number: ";
cin >> num1;
// Read the first number from the user
cout << "Enter second number: ";
cin >> num2;
// Read the second number from the user
// Display the numbers before swapping
cout << "Before swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
// Swap the numbers using a temporary variable
temp = num1;
// Store the value of num1 in temp
num1 = num2;
// Assign the value of num2 to num1
num2 = temp;
// Assign the value of temp (original num1) to num2
// Display the numbers after swapping
cout << "After swapping: " << endl;
cout << "num1 = " << num1 << ", num2 = " << num2 << endl;
return 0;
// The program returns 0 to indicate that it ended successfully
}
Description
- Preprocessor Directive:
- #include
- This line tells the preprocessor to include the iostream library, which is necessary for input and output operations in C++.
- #include
- Namespace Declaration:
- using namespace std;
- The std namespace includes all the features of the C++ Standard Library. By including this line, we don’t need to prefix standard library names with std::.
- using namespace std;
- Main Function:
- int main() { … }
- The main function is the starting point of every C++ program. It returns an integer value, which is typically used to indicate whether the program ended successfully.
- int main() { … }
- Variable Declarations:
- int num1, num2, temp;
- These lines declare three integer variables: num1, num2, and temp.
- int num1, num2, temp;
- Input Statements:
- cout << “Enter first number: “;
- This line prints a prompt to the console asking the user to enter the first number.
- cin >> num1;
- This line reads an integer input from the user and stores it in the num1 variable.
- cout << “Enter second number: “;
- This line prints a prompt to the console asking the user to enter the second number.
- cin >> num2;
- This line reads an integer input from the user and stores it in the num2 variable.
- cout << “Enter first number: “;
- Display Before Swapping:
- cout << “Before swapping: ” << endl;
- This line prints a message indicating that the numbers displayed next are before swapping.
- cout << “num1 = ” << num1 << “, num2 = ” << num2 << endl;
- These lines print the values of num1 and num2 before swapping.
- cout << “Before swapping: ” << endl;
- Swapping Logic:
- temp = num1;
- This line stores the value of num1 in the temp variable.
- num1 = num2;
- This line assigns the value of num2 to num1.
- num2 = temp;
- This line assigns the value of temp (which holds the original value of num1) to num2.
- temp = num1;
- Display After Swapping:
- cout << “After swapping: ” << endl;
- This line prints a message indicating that the numbers displayed next are after swapping.
- cout << “num1 = ” << num1 << “, num2 = ” << num2 << endl;
- These lines print the values of num1 and num2 after swapping.
- cout << “After swapping: ” << endl;
- Return Statement:
- return 0;
- This statement ends the main function and returns the value 0 to the operating system. A return value of 0 typically indicates that the program executed successfully.
- return 0;
This program takes two integers as input from the user, swaps their values using a temporary variable, and displays the numbers before and after swapping.
Leave a Reply