Skip to content

Commit b88af07

Browse files
kde0820egonSchiele
authored andcommitted
Add C example for recursion (egonSchiele#41)
1 parent 38d5041 commit b88af07

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

03_recursion/c/01_countdown.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <stdio.h>
2+
3+
void countdown(int i) {
4+
printf("%d\n", i);
5+
6+
// base case
7+
if (i <= 0)
8+
return;
9+
//recursive case
10+
else
11+
countdown(i - 1);
12+
}
13+
14+
int main(void) {
15+
16+
countdown(5);
17+
18+
return 0;
19+
}

03_recursion/c/02_greet.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <stdio.h>
2+
3+
void greet2(char *name) {
4+
printf("how are you, %s?\n", name);
5+
}
6+
7+
void bye() {
8+
printf("ok bye!\n");
9+
}
10+
11+
void greet(char *name) {
12+
printf("hello, %s!\n", name);
13+
greet2(name);
14+
printf("getting ready to say bye...\n");
15+
bye();
16+
}
17+
18+
int main(void) {
19+
20+
greet("adit");
21+
22+
return 0;
23+
}

03_recursion/c/03_factorial.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <stdio.h>
2+
3+
int fact(int x) {
4+
if (x == 1)
5+
return 1;
6+
else
7+
return x * fact(x - 1);
8+
}
9+
10+
int main(void) {
11+
12+
printf("%d", fact(5));
13+
14+
return 0;
15+
}

0 commit comments

Comments
 (0)