Skip to content

Commit 6e3e6a9

Browse files
authored
Update code.md
1 parent 001256c commit 6e3e6a9

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

code.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,4 +211,35 @@ int main() {
211211
}
212212
```
213213

214+
## Υπολογισμός παραγοντικού / factorial - χρήση αναδρομής - lec11
215+
216+
Credits: Στέφανος
217+
218+
```c
219+
/* Short program for computing the factorial
220+
of a number. Does not handle edge-cases
221+
like negative numbers. */
222+
#include <stdio.h>
223+
#include <stdlib.h>
224+
225+
int factorial(int num) {
226+
printf("Computing: factorial(%d)\n", num);
227+
if (num <= 1) {
228+
return 1;
229+
} else {
230+
return num * factorial(num - 1);
231+
}
232+
}
233+
234+
int main(int argc, char ** argv) {
235+
if (argc != 2) {
236+
printf("Run this program as ./factorial <num>\n");
237+
return 1;
238+
}
239+
int num = atoi(argv[1]);
240+
printf("The factorial of this number is: %d\n", factorial(num));
241+
return 0;
242+
}
243+
```
244+
214245

0 commit comments

Comments
 (0)