From b454718be99ebd4612359c07d240f9f4ef360dd4 Mon Sep 17 00:00:00 2001 From: Jagrata Deb Date: Tue, 4 Feb 2025 11:38:18 +0530 Subject: [PATCH] Commit --- .vscode/settings.json | 6 +++++ .../2 Types of Recursion/headRecursion.cpp | 4 ++-- .../2 Types of Recursion/tailRecursion.cpp | 3 +-- .../2 Types of Recursion/treeRecursion.cpp | 24 +++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 Basic Concept/7 Recursion/2 Types of Recursion/treeRecursion.cpp diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..48bd760 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "files.associations": { + "ostream": "cpp", + "iostream": "cpp" + } +} \ No newline at end of file diff --git a/Basic Concept/7 Recursion/2 Types of Recursion/headRecursion.cpp b/Basic Concept/7 Recursion/2 Types of Recursion/headRecursion.cpp index 7ce57b5..394f28e 100644 --- a/Basic Concept/7 Recursion/2 Types of Recursion/headRecursion.cpp +++ b/Basic Concept/7 Recursion/2 Types of Recursion/headRecursion.cpp @@ -3,10 +3,10 @@ using namespace std; void fun(int n) { - // If n is greater than 0, print n and call fun(n - 1) + // If n is greater than 0, call fun(n - 1) and print n if (n > 0) { - fun(n - 1); // tail recursion + fun(n - 1); // Head recursion: The function calls itself before executing any other statements cout << n << endl; } } diff --git a/Basic Concept/7 Recursion/2 Types of Recursion/tailRecursion.cpp b/Basic Concept/7 Recursion/2 Types of Recursion/tailRecursion.cpp index f96230a..ae02d35 100644 --- a/Basic Concept/7 Recursion/2 Types of Recursion/tailRecursion.cpp +++ b/Basic Concept/7 Recursion/2 Types of Recursion/tailRecursion.cpp @@ -7,7 +7,7 @@ void fun(int n) if (n > 0) { cout << n << endl; - fun(n - 1); // tail recursion + fun(n - 1); // Tail recursion: The function calls itself as the last action in its code block } } @@ -18,6 +18,5 @@ int main() cout << "Output:" << endl; fun(n); // Call the function with n = 3 - return 0; } diff --git a/Basic Concept/7 Recursion/2 Types of Recursion/treeRecursion.cpp b/Basic Concept/7 Recursion/2 Types of Recursion/treeRecursion.cpp new file mode 100644 index 0000000..6a70f2c --- /dev/null +++ b/Basic Concept/7 Recursion/2 Types of Recursion/treeRecursion.cpp @@ -0,0 +1,24 @@ +#include // Include the standard input-output stream library +using namespace std; // Use the standard namespace + +// Function definition for 'fun' which performs tree recursion +void fun(int n) +{ + if (n > 0) // Base case: when n is greater than 0 + { + cout << n; // Print the current value of n + fun(n - 1); // Recursive call to 'fun' with n-1 + fun(n - 1); // Another recursive call to 'fun' with n-1 + } +} + +int main() +{ + int n = 3; // Initialize n with a value of 3 + fun(n); // Call the 'fun' function with n + cout << "\nThis is an example of Tree Recursion" << endl; // Print a message + cout << "Output:" << endl; // Print a label for the output + return 0; // End of the main function +} + +// Output: 3211211