From ae9d0bd482ec7a835ee5deab7bfaf1de5e66d774 Mon Sep 17 00:00:00 2001 From: Prince Sharma Date: Mon, 28 Apr 2025 20:10:37 +0530 Subject: [PATCH 1/2] Add C++ program to swap two numbers --- math/swap_two_numbers.cpp | 63 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 math/swap_two_numbers.cpp diff --git a/math/swap_two_numbers.cpp b/math/swap_two_numbers.cpp new file mode 100644 index 00000000000..2e1f56a21f5 --- /dev/null +++ b/math/swap_two_numbers.cpp @@ -0,0 +1,63 @@ +#include +#include + +/** + * Function to swap two numbers using reference variables. + * @param a First number. + * @param b Second number. + * + * \detail + * The algorithm uses a temporary variable to store the value of a, + * then assigns the value of b to a, and finally assigns the value + * stored in the temporary variable to b. + */ +void swap_two_numbers(int &a, int &b) +{ + int temp = a; + a = b; + b = temp; +} + +/** + * Function for testing the swap_two_numbers() function with a + * first test case of 5 and 10 and assert statement. + */ +void test1() +{ + int a = 5, b = 10; + swap_two_numbers(a, b); + assert(a == 10 && b == 5); +} + +/** + * Function for testing the swap_two_numbers() function with a + * second test case of -5 and 10 and assert statement. + */ +void test2() +{ + int a = -5, b = 10; + swap_two_numbers(a, b); + assert(a == 10 && b == -5); +} + +/** + * Function for testing the swap_two_numbers() with + * all the test cases. + */ +void test() +{ + // First test. + test1(); + // Second test. + test2(); +} + +/** + * Main Function + */ +int main() +{ + test(); + std::cout << "Success." << std::endl; + return 0; +} From 869593a9d484d4a19ae7dc690428a88680843c50 Mon Sep 17 00:00:00 2001 From: Prince Sharma Date: Mon, 28 Apr 2025 23:27:06 +0530 Subject: [PATCH 2/2] Add C++ program to swap two numbers --- math/swap_two_numbers.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/math/swap_two_numbers.cpp b/math/swap_two_numbers.cpp index 2e1f56a21f5..29a6edeb33f 100644 --- a/math/swap_two_numbers.cpp +++ b/math/swap_two_numbers.cpp @@ -1,3 +1,9 @@ +/** + * Copyright 2025 @author princesharma2004 + * + * @file + * \brief A C++ Program to swap two numbers using reference variables. + */ #include #include