Skip to content

Commit e95409b

Browse files
divyakhetanMadhavBahl
authored andcommitted
Day 13,14,15 (#158)
* day13 code and readme * day14 code and readme * day15 code and readme
1 parent 60ab08d commit e95409b

File tree

8 files changed

+268
-0
lines changed

8 files changed

+268
-0
lines changed

day13/C++/factorialDay13.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @author:divyakhetan
3+
* @date: 10/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
int fact(int n){
11+
if(n == 1) return 1;
12+
else return n * fact(n - 1);
13+
}
14+
15+
16+
int main(){
17+
18+
int n;
19+
cin >> n;
20+
21+
cout << "The factorial is " << fact(n);
22+
23+
24+
return 0;
25+
}

day13/C++/fibonacciDay13.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @author:divyakhetan
3+
* @date: 10/1/2019
4+
*/
5+
6+
#include<bits/stdc++.h>
7+
using namespace std;
8+
9+
10+
int fibo(int n){
11+
if(n <= 2) return 1;
12+
else return fibo(n - 1) + fibo(n - 2);
13+
}
14+
int main(){
15+
16+
int n;
17+
cin >> n;
18+
19+
cout << "The " << n << "th fibonacci number is " << fibo(n) << endl;
20+
21+
for(int i = 1; i <= n; i++){
22+
if(i == n){
23+
cout << fibo(i);
24+
}
25+
else{
26+
cout << fibo(i) << " , ";
27+
}
28+
}
29+
return 0;
30+
}

day13/README.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,37 @@ int main()
115115
}
116116
```
117117
118+
#### [Solution](./C++/factorialDay13.cpp)
119+
120+
```cpp
121+
/**
122+
* @author:divyakhetan
123+
* @date: 10/1/2019
124+
*/
125+
126+
#include<bits/stdc++.h>
127+
using namespace std;
128+
129+
130+
int fact(int n){
131+
if(n == 1) return 1;
132+
else return n * fact(n - 1);
133+
}
134+
135+
136+
int main(){
137+
138+
int n;
139+
cin >> n;
140+
141+
cout << "The factorial is " << fact(n);
142+
143+
144+
return 0;
145+
}
146+
```
147+
148+
118149
### C Implementation
119150

120151
#### [Solution](./C/factorial.c)
@@ -423,6 +454,42 @@ int main()
423454
}
424455
```
425456
457+
#### [Solution](./C++/fibonacciDay13.cpp)
458+
459+
```cpp
460+
/**
461+
* @author:divyakhetan
462+
* @date: 10/1/2019
463+
*/
464+
465+
#include<bits/stdc++.h>
466+
using namespace std;
467+
468+
469+
int fibo(int n){
470+
if(n <= 2) return 1;
471+
else return fibo(n - 1) + fibo(n - 2);
472+
}
473+
int main(){
474+
475+
int n;
476+
cin >> n;
477+
478+
cout << "The " << n << "th fibonacci number is " << fibo(n) << endl;
479+
480+
for(int i = 1; i <= n; i++){
481+
if(i == n){
482+
cout << fibo(i);
483+
}
484+
else{
485+
cout << fibo(i) << " , ";
486+
}
487+
}
488+
return 0;
489+
}
490+
491+
492+
426493
### C Implementation
427494
428495
#### [Solution](./C/fibonacci.c)

day14/C++/productDay14.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @author:divyakhetan
3+
* @date: 10/1/2019
4+
*/
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
11+
int product(int a, int b){
12+
if(b == 0) return 0; // base case;
13+
int ans = product(a, b/2);
14+
if(b % 2 == 0) return 2 * ans;
15+
else return 2 * ans + a;
16+
}
17+
int main(){
18+
int num1, num2;
19+
cin >> num1 >> num2;
20+
int ans = product(num1, num2);
21+
//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.
22+
if( (num2 < 0 && num1 > 0) || (num2 < 0 && num1 < 0)) ans *= -1;
23+
cout << "The multiplication is " << ans;
24+
}

day14/C++/sumOfDigitsDay14.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @author:divyakhetan
3+
* @date: 10/1/2019
4+
*/
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
11+
int sum(int n){
12+
if(n < 10) return n;
13+
else return n % 10 + sum(n /10);
14+
}
15+
int main(){
16+
int n;
17+
cin >> n;
18+
cout << "The sum of digits is " << sum(n);
19+
return 0;
20+
}

day14/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,30 @@ int main() {
162162
}
163163
```
164164

165+
#### [C++ Solution by @divyakhetan](./C++/sumOfDigitsDay14.cpp)
166+
```cpp
167+
/**
168+
* @author:divyakhetan
169+
* @date: 10/1/2019
170+
*/
171+
172+
173+
#include<bits/stdc++.h>
174+
using namespace std;
175+
176+
177+
int sum(int n){
178+
if(n < 10) return n;
179+
else return n % 10 + sum(n /10);
180+
}
181+
int main(){
182+
int n;
183+
cin >> n;
184+
cout << "The sum of digits is " << sum(n);
185+
return 0;
186+
}
187+
```
188+
165189
### Ruby Implementation
166190
167191
#### [Solution](./Ruby/sum_of_digits.rb)
@@ -369,6 +393,30 @@ int main() {
369393
}
370394
```
371395
396+
#### [C++ Solution by @divyakhetan](./C++/productDay14.cpp)
397+
```cpp
398+
/**
399+
* @author:divyakhetan
400+
* @date: 10/1/2019
401+
*/
402+
403+
404+
#include<bits/stdc++.h>
405+
using namespace std;
406+
407+
408+
int sum(int n){
409+
if(n < 10) return n;
410+
else return n % 10 + sum(n /10);
411+
}
412+
int main(){
413+
int n;
414+
cin >> n;
415+
cout << "The sum of digits is " << sum(n);
416+
return 0;
417+
}
418+
```
419+
372420
### Ruby Implementation
373421

374422
#### [Solution](./Ruby/product_of_two_numbers.rb)

day15/C++/pascalDay15.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @author:divyakhetan
3+
* @date: 10/1/2019
4+
*/
5+
6+
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
10+
int pascal(int i, int j){
11+
if(j == 1) return 1;
12+
if(i == j ) return 1;
13+
else return pascal(i - 1, j) + pascal(i - 1, j - 1);
14+
}
15+
16+
int main(){
17+
int n;
18+
cin >> n;
19+
20+
for(int i = 1; i <= n; i++){
21+
for(int j = 1; j <= i; j++){
22+
cout << pascal(i,j) << " ";
23+
}
24+
cout << endl;
25+
}
26+
return 0;
27+
}

day15/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ console.log ('\n/* ===== Pascal\'s Triangle for n = 7\n');
5555
printPascal (7);
5656
```
5757

58+
## C++ Implementation
59+
60+
### [Solution](./C++/pascalDay15.cpp)
61+
62+
```cpp
63+
/**
64+
* @author:divyakhetan
65+
* @date: 10/1/2019
66+
*/
67+
68+
69+
#include<bits/stdc++.h>
70+
using namespace std;
71+
72+
73+
int sum(int n){
74+
if(n < 10) return n;
75+
else return n % 10 + sum(n /10);
76+
}
77+
int main(){
78+
int n;
79+
cin >> n;
80+
cout << "The sum of digits is " << sum(n);
81+
return 0;
82+
}
83+
```
84+
5885
## Python Implementation
5986
6087
### [Solution] (./Python/pascal.py)

0 commit comments

Comments
 (0)