From c82cbee76d1ffeb2e0e57ae62a0890de61415102 Mon Sep 17 00:00:00 2001 From: Jishan Shaikh Date: Sat, 11 Sep 2021 20:50:48 +0530 Subject: [PATCH] chore(CPlusPlus): adding recursion programs (#402) Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com> --- algorithms/CPlusPlus/README.md | 4 +++ .../Recursion/GCD-of-two-numbers.cpp | 20 ++++++++++++++ .../Sum-of-all-elements-in-an-array.cpp | 18 +++++++++++++ .../decimal-to-binary-conversion.cpp | 26 +++++++++++++++++++ .../Recursion/sum-of-digits-of-an-integer.cpp | 19 ++++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 algorithms/CPlusPlus/Recursion/GCD-of-two-numbers.cpp create mode 100644 algorithms/CPlusPlus/Recursion/Sum-of-all-elements-in-an-array.cpp create mode 100644 algorithms/CPlusPlus/Recursion/decimal-to-binary-conversion.cpp create mode 100644 algorithms/CPlusPlus/Recursion/sum-of-digits-of-an-integer.cpp diff --git a/algorithms/CPlusPlus/README.md b/algorithms/CPlusPlus/README.md index 2095213f..393d9be7 100644 --- a/algorithms/CPlusPlus/README.md +++ b/algorithms/CPlusPlus/README.md @@ -119,3 +119,7 @@ 1. [Tower of Hanoi](Recursion/towerofHanoi.cpp) 2. [Factorial](Recursion/factorial.cpp) 3. [Permutation](Recursion/permutation.cpp) +4. [GCD/HCF of two numbers](Recursion/GCD-of-two-numbers.cpp) +5. [Sum of all elements of an array](Recursion/Sum-of-all-elements-in-an-array.cpp) +6. [Decimal number to Binary conversion](Recursion/decimal-to-binary-conversion.cpp) +7. [Sum of digits of a decimal integer](Recursion/sum-of-digits-of-an-integer.cpp) diff --git a/algorithms/CPlusPlus/Recursion/GCD-of-two-numbers.cpp b/algorithms/CPlusPlus/Recursion/GCD-of-two-numbers.cpp new file mode 100644 index 00000000..c3c0a990 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/GCD-of-two-numbers.cpp @@ -0,0 +1,20 @@ +// Program to find GCD of two numbers using recursion +// Executable link: https://ide.geeksforgeeks.org/wixyiKNFMi + +#include + +using namespace std; + +// Also known as Euclidean algorithm +// There exists an inbuilt function for gcd also __gcd(T a, T b) with Template T +int euclidean_gcd(int a, int b) { + if (b == 0) + return a; + return euclidean_gcd(b, a%b); +} + +int main() { + int n1 = 56, n2 = 65; + cout << "GCD of " << n1 << " and " << n2 << " is " << euclidean_gcd(n1, n2); + return 0; +} diff --git a/algorithms/CPlusPlus/Recursion/Sum-of-all-elements-in-an-array.cpp b/algorithms/CPlusPlus/Recursion/Sum-of-all-elements-in-an-array.cpp new file mode 100644 index 00000000..f9a0ccef --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/Sum-of-all-elements-in-an-array.cpp @@ -0,0 +1,18 @@ +// Program to find sum of all integer elements in an array +// Executable link: https://ide.geeksforgeeks.org/cey3HeTjYK + +#include + +using namespace std; + +int findSum(int A[], int size) { + if (n <= 0) + return 0; + return (findSum(A, size-1) + A[size-1]); +} + +int main() { + int A[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; + cout << "The sum of the array is " << findSum(A, 10); + return 0; +} diff --git a/algorithms/CPlusPlus/Recursion/decimal-to-binary-conversion.cpp b/algorithms/CPlusPlus/Recursion/decimal-to-binary-conversion.cpp new file mode 100644 index 00000000..5491db17 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/decimal-to-binary-conversion.cpp @@ -0,0 +1,26 @@ +// Program to convert Decimal to Binary +// Negative numbers are expressed here as Signed magnitude representation +// Executable link: https://ide.geeksforgeeks.org/Cubhhp1jIl + +#include + +using namespace std; + +// Trap: Remember the limit of int in C++ +// if a binary of a number exceed the limit of int, it will overflow +// Tip: Use long long (and unsigned long long) instead +unsigned long long decimalToBinary (int decimal_number) { + if (decimal_number == 0) + return 0; + else + return (decimal_number%2 + 10*decimalToBinary(decimal_number/2)); +} + +int main() { + int numb = 5; + int signed_bit = 0; + if(numb < 0) + signed_bit = 1; + cout << "Binary of " << numb << " is " << signed_bit << decimalToBinary(numb); + return 0; +} diff --git a/algorithms/CPlusPlus/Recursion/sum-of-digits-of-an-integer.cpp b/algorithms/CPlusPlus/Recursion/sum-of-digits-of-an-integer.cpp new file mode 100644 index 00000000..0b0e5927 --- /dev/null +++ b/algorithms/CPlusPlus/Recursion/sum-of-digits-of-an-integer.cpp @@ -0,0 +1,19 @@ +// Program to find sum of digits of an integer (Decimal Integer) +// Executable link: https://ide.geeksforgeeks.org/pHHmcxVNKZ + +#include + +using namespace std; + +// Replace 10 by 2 for binary numbers, 8 for octal numbers +int sum_of_digits(int n) { + if (n == 0) + return 0; + return (n%10 + sum_of_digits(n/10)); +} + +int main() { + int numb = 12345; + cout << "Sum of digits of " << numb << " is " << sum_of_digits(numb); + return 0; +}