diff --git a/day13/C++/factorialDay13.cpp b/day13/C++/factorialDay13.cpp new file mode 100644 index 00000000..97ca2a29 --- /dev/null +++ b/day13/C++/factorialDay13.cpp @@ -0,0 +1,25 @@ +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + +#include +using namespace std; + + +int fact(int n){ + if(n == 1) return 1; + else return n * fact(n - 1); +} + + +int main(){ + + int n; + cin >> n; + + cout << "The factorial is " << fact(n); + + + return 0; +} diff --git a/day13/C++/fibonacciDay13.cpp b/day13/C++/fibonacciDay13.cpp new file mode 100644 index 00000000..39801da7 --- /dev/null +++ b/day13/C++/fibonacciDay13.cpp @@ -0,0 +1,30 @@ +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + +#include +using namespace std; + + +int fibo(int n){ + if(n <= 2) return 1; + else return fibo(n - 1) + fibo(n - 2); +} +int main(){ + + int n; + cin >> n; + + cout << "The " << n << "th fibonacci number is " << fibo(n) << endl; + + for(int i = 1; i <= n; i++){ + if(i == n){ + cout << fibo(i); + } + else{ + cout << fibo(i) << " , "; + } + } + return 0; +} diff --git a/day13/README.md b/day13/README.md index 7d3ca23d..06ea907f 100644 --- a/day13/README.md +++ b/day13/README.md @@ -115,6 +115,37 @@ int main() } ``` +#### [Solution](./C++/factorialDay13.cpp) + +```cpp +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + +#include +using namespace std; + + +int fact(int n){ + if(n == 1) return 1; + else return n * fact(n - 1); +} + + +int main(){ + + int n; + cin >> n; + + cout << "The factorial is " << fact(n); + + + return 0; +} +``` + + ### C Implementation #### [Solution](./C/factorial.c) @@ -423,6 +454,42 @@ int main() } ``` +#### [Solution](./C++/fibonacciDay13.cpp) + +```cpp +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + +#include +using namespace std; + + +int fibo(int n){ + if(n <= 2) return 1; + else return fibo(n - 1) + fibo(n - 2); +} +int main(){ + + int n; + cin >> n; + + cout << "The " << n << "th fibonacci number is " << fibo(n) << endl; + + for(int i = 1; i <= n; i++){ + if(i == n){ + cout << fibo(i); + } + else{ + cout << fibo(i) << " , "; + } + } + return 0; +} + + + ### C Implementation #### [Solution](./C/fibonacci.c) diff --git a/day14/C++/productDay14.cpp b/day14/C++/productDay14.cpp new file mode 100644 index 00000000..b473acfb --- /dev/null +++ b/day14/C++/productDay14.cpp @@ -0,0 +1,24 @@ +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + + +int product(int a, int b){ + if(b == 0) return 0; // base case; + int ans = product(a, b/2); + if(b % 2 == 0) return 2 * ans; + else return 2 * ans + a; +} +int main(){ + int num1, num2; + cin >> num1 >> num2; + int ans = product(num1, num2); + //since we are adding the first number in the recursive function, we get the ans only according to the num1, hence we need to adjust the sign accd to the num2. + if( (num2 < 0 && num1 > 0) || (num2 < 0 && num1 < 0)) ans *= -1; + cout << "The multiplication is " << ans; +} diff --git a/day14/C++/sumOfDigitsDay14.cpp b/day14/C++/sumOfDigitsDay14.cpp new file mode 100644 index 00000000..4fc85f3d --- /dev/null +++ b/day14/C++/sumOfDigitsDay14.cpp @@ -0,0 +1,20 @@ +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + + +int sum(int n){ + if(n < 10) return n; + else return n % 10 + sum(n /10); +} +int main(){ + int n; + cin >> n; + cout << "The sum of digits is " << sum(n); + return 0; +} diff --git a/day14/README.md b/day14/README.md index 5cdfc0f0..af3bc76d 100644 --- a/day14/README.md +++ b/day14/README.md @@ -162,6 +162,30 @@ int main() { } ``` +#### [C++ Solution by @divyakhetan](./C++/sumOfDigitsDay14.cpp) +```cpp +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + + +int sum(int n){ + if(n < 10) return n; + else return n % 10 + sum(n /10); +} +int main(){ + int n; + cin >> n; + cout << "The sum of digits is " << sum(n); + return 0; +} +``` + ### Ruby Implementation #### [Solution](./Ruby/sum_of_digits.rb) @@ -369,6 +393,30 @@ int main() { } ``` +#### [C++ Solution by @divyakhetan](./C++/productDay14.cpp) +```cpp +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + + +int sum(int n){ + if(n < 10) return n; + else return n % 10 + sum(n /10); +} +int main(){ + int n; + cin >> n; + cout << "The sum of digits is " << sum(n); + return 0; +} +``` + ### Ruby Implementation #### [Solution](./Ruby/product_of_two_numbers.rb) diff --git a/day15/C++/pascalDay15.cpp b/day15/C++/pascalDay15.cpp new file mode 100644 index 00000000..6856e7b4 --- /dev/null +++ b/day15/C++/pascalDay15.cpp @@ -0,0 +1,27 @@ +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + +int pascal(int i, int j){ + if(j == 1) return 1; + if(i == j ) return 1; + else return pascal(i - 1, j) + pascal(i - 1, j - 1); +} + +int main(){ + int n; + cin >> n; + + for(int i = 1; i <= n; i++){ + for(int j = 1; j <= i; j++){ + cout << pascal(i,j) << " "; + } + cout << endl; + } + return 0; +} diff --git a/day15/README.md b/day15/README.md index c6db4886..c7c4a70e 100644 --- a/day15/README.md +++ b/day15/README.md @@ -55,6 +55,33 @@ console.log ('\n/* ===== Pascal\'s Triangle for n = 7\n'); printPascal (7); ``` +## C++ Implementation + +### [Solution](./C++/pascalDay15.cpp) + +```cpp +/** + * @author:divyakhetan + * @date: 10/1/2019 + */ + + +#include +using namespace std; + + +int sum(int n){ + if(n < 10) return n; + else return n % 10 + sum(n /10); +} +int main(){ + int n; + cin >> n; + cout << "The sum of digits is " << sum(n); + return 0; +} +``` + ## Python Implementation ### [Solution] (./Python/pascal.py)