Skip to content

Commit

Permalink
Day 13,14,15 (#158)
Browse files Browse the repository at this point in the history
* day13 code and readme

* day14 code and readme

* day15 code and readme
  • Loading branch information
divyakhetan authored and MadhavBahl committed Jan 12, 2019
1 parent 60ab08d commit e95409b
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 0 deletions.
25 changes: 25 additions & 0 deletions day13/C++/factorialDay13.cpp
@@ -0,0 +1,25 @@
/**
* @author:divyakhetan
* @date: 10/1/2019
*/

#include<bits/stdc++.h>
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;
}
30 changes: 30 additions & 0 deletions day13/C++/fibonacciDay13.cpp
@@ -0,0 +1,30 @@
/**
* @author:divyakhetan
* @date: 10/1/2019
*/

#include<bits/stdc++.h>
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;
}
67 changes: 67 additions & 0 deletions day13/README.md
Expand Up @@ -115,6 +115,37 @@ int main()
}
```
#### [Solution](./C++/factorialDay13.cpp)
```cpp
/**
* @author:divyakhetan
* @date: 10/1/2019
*/
#include<bits/stdc++.h>
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)
Expand Down Expand Up @@ -423,6 +454,42 @@ int main()
}
```
#### [Solution](./C++/fibonacciDay13.cpp)
```cpp
/**
* @author:divyakhetan
* @date: 10/1/2019
*/
#include<bits/stdc++.h>
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)
Expand Down
24 changes: 24 additions & 0 deletions day14/C++/productDay14.cpp
@@ -0,0 +1,24 @@
/**
* @author:divyakhetan
* @date: 10/1/2019
*/


#include<bits/stdc++.h>
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;
}
20 changes: 20 additions & 0 deletions day14/C++/sumOfDigitsDay14.cpp
@@ -0,0 +1,20 @@
/**
* @author:divyakhetan
* @date: 10/1/2019
*/


#include<bits/stdc++.h>
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;
}
48 changes: 48 additions & 0 deletions day14/README.md
Expand Up @@ -162,6 +162,30 @@ int main() {
}
```

#### [C++ Solution by @divyakhetan](./C++/sumOfDigitsDay14.cpp)
```cpp
/**
* @author:divyakhetan
* @date: 10/1/2019
*/


#include<bits/stdc++.h>
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)
Expand Down Expand Up @@ -369,6 +393,30 @@ int main() {
}
```
#### [C++ Solution by @divyakhetan](./C++/productDay14.cpp)
```cpp
/**
* @author:divyakhetan
* @date: 10/1/2019
*/
#include<bits/stdc++.h>
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)
Expand Down
27 changes: 27 additions & 0 deletions day15/C++/pascalDay15.cpp
@@ -0,0 +1,27 @@
/**
* @author:divyakhetan
* @date: 10/1/2019
*/


#include<bits/stdc++.h>
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;
}
27 changes: 27 additions & 0 deletions day15/README.md
Expand Up @@ -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<bits/stdc++.h>
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)
Expand Down

0 comments on commit e95409b

Please sign in to comment.