Add C example for recursion (#41)

This commit is contained in:
kde0820
2017-11-14 01:11:41 +09:00
committed by Aditya Bhargava
parent 38d50415e8
commit b88af07fd0
3 changed files with 57 additions and 0 deletions

View File

@@ -0,0 +1,19 @@
#include <stdio.h>
void countdown(int i) {
printf("%d\n", i);
// base case
if (i <= 0)
return;
//recursive case
else
countdown(i - 1);
}
int main(void) {
countdown(5);
return 0;
}

23
03_recursion/c/02_greet.c Normal file
View File

@@ -0,0 +1,23 @@
#include <stdio.h>
void greet2(char *name) {
printf("how are you, %s?\n", name);
}
void bye() {
printf("ok bye!\n");
}
void greet(char *name) {
printf("hello, %s!\n", name);
greet2(name);
printf("getting ready to say bye...\n");
bye();
}
int main(void) {
greet("adit");
return 0;
}

View File

@@ -0,0 +1,15 @@
#include <stdio.h>
int fact(int x) {
if (x == 1)
return 1;
else
return x * fact(x - 1);
}
int main(void) {
printf("%d", fact(5));
return 0;
}