chore(CPlusPlus): adding recursion programs (#402)

Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>
pull/457/head^2
Jishan Shaikh 2021-09-11 20:50:48 +05:30 committed by GitHub
parent 25817272ca
commit c82cbee76d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 87 additions and 0 deletions

View File

@ -119,3 +119,7 @@
1. [Tower of Hanoi](Recursion/towerofHanoi.cpp) 1. [Tower of Hanoi](Recursion/towerofHanoi.cpp)
2. [Factorial](Recursion/factorial.cpp) 2. [Factorial](Recursion/factorial.cpp)
3. [Permutation](Recursion/permutation.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)

View File

@ -0,0 +1,20 @@
// Program to find GCD of two numbers using recursion
// Executable link: https://ide.geeksforgeeks.org/wixyiKNFMi
#include <iostream>
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;
}

View File

@ -0,0 +1,18 @@
// Program to find sum of all integer elements in an array
// Executable link: https://ide.geeksforgeeks.org/cey3HeTjYK
#include <iostream>
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;
}

View File

@ -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 <iostream>
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;
}

View File

@ -0,0 +1,19 @@
// Program to find sum of digits of an integer (Decimal Integer)
// Executable link: https://ide.geeksforgeeks.org/pHHmcxVNKZ
#include <iostream>
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;
}